-1

当一个人遍历一个链接结构以作用于该结构时(即:将一个节点插入一个简单的链表作为一个简单的例子),通过将一个双指针推入该结构来获得最佳算法。如果使用单个引用,则必须为空根和/或尾部插入编写一个或多个特殊情况。

node_type **dp = &root;

while(*dp && /insertion point not reached/)
dp=&(*dp)->next;

当我跳出循环时, *dp 是插入列表的点;我持有对对象链接的引用。此引用可能指向根、结构末尾的空对象或任何其他节点。随着结构变得更加复杂,对双重参考的需求变得更加明显,因为对特殊情况的需求往往呈指数增长。

如何在 Visual Basic 中实现双重引用?

注意:链表位仅作为示例...我知道:解决这个简单问题有很多方法。

4

2 回答 2

1

请原谅 C#,我的 VB.NET 相当生锈。在 C#/VB.NET 中安全地进行双重引用的唯一方法是使用ref方法的参数。

using System;

namespace Test
{
    class Program
    {
        private static void Main(string[] args)
        {
            // Create an example root node. This example still works when root
            // is null.
            Node root = new Node() { next = new Node() };

            // Setup a Node that contains a pointer to root. This variable will
            // not be destroyed by iteration and rp.next will always be root
            // after iteration, even if root started as null.
            Node rp = new Node() { next = root };

            // Initialize the iterator to the root pointer.
            Node dp = rp;

            // Define a new node to be inserted for this example.
            Node nn1 = new Node();
            Node nn2 = new Node();

            // Iterate by calling DoWork until a null reference is returned.
            // Note that a reference to dp.next is passed to the method. This
            // allows the method to change what dp.next points to. The node to
            // insert is also given to the method. You could easily change this
            // second parameter to be an action that works on the desired node.
            while(null != (dp = DoWork(ref dp.next, nn1))) { }
            dp = rp;
            while(null != (dp = DoWork(ref dp.next, nn2))) { }

            // Force root to be assigned. If root did not start as null this
            // line is unnecessary.
            root = rp.next;
        }

        private static Node DoWork(ref Node node, Node nn)
        {
            // Logic to determine if the insertion point is not reached. For
            // this example the new node is always inserted just before the tail.
            bool logic = null != node && null != node.next;

            // Check node and logic. Return the node to continue iterating.
            if(null != node && logic)
            {
                return node;
            }

            // Insertion logic here. For the example the new node is inserted
            // into the linked list.
            nn.next = node;
            node = nn;

            // Return null to signify that iteration has completed.
            return null;
        }
    }

    [System.Diagnostics.DebuggerDisplay("{id} - {next}")]
    class Node
    {
        private static int sID;

        public int id;
        public Node next;

        public Node()
        {
            id = ++sID;
        }
    }
}
于 2013-05-02T19:49:57.090 回答
-1

经过研究,我得出结论,答案类似于:“如果你喝得太醉而不能开车,但你真的需要开车怎么办?” 答:很简单,不要开车。

垃圾收集语言,例如:Visual Basic,不允许区分收集的变量和对其的引用。我需要双重参考吗?我当然是了!有没有办法用垃圾收集语言获得一个?不......如果我能做这样的事情,它会破坏垃圾收集器。

好吧,我希望有一些神奇的答案……喝很多咖啡?我只需要接受这样一个事实,即在 Visual Basic 中没有像“双重引用”这样的动物,并找到解决它的方法。

史密斯

于 2013-05-04T18:40:33.820 回答