C# 上一个和下一个双精度无法连接



我正在学习一些关于链表和双链表的知识。

我试图用上一个和下一个节点构建一个双链表,但是当我对程序进行解码时,"指针"不会在它们应该连接的地方相互连接。 在程序运行结束时,打印出的唯一内容是我想输入列表的最后一个值,该值是列表中唯一的值。

这是我的代码,我相信它应该没问题,因为"上一个"和"下一个"节点不会相互连接。

Node<T> start;
    Node<T> end;
    public void AddFirst(T dataToAdd)
    {
        Node<T> tmp = new Node<T>(dataToAdd);
        if (start == null)
        {
            start = tmp;
            end = start;
        }
        tmp.next = start.previous;
        end.next = tmp.previous;
        start = tmp;

        if (start.next == null)
        {
            end = start;
        }
    }
    public void AddLast(T dataToAdd)
    {
        Node<T> tmp = new Node<T>(dataToAdd);
        if (start == null)
        {
            AddFirst(dataToAdd);
        }
        tmp.next = start.previous;
        end.next = tmp.previous;
        end = tmp;
    }
    public T RemoveFirst()
    {
        if (start == null) return default(T);
        T saveVal = start.data;
        end.next = start.next.previous;
        start = start.next;
        if (start == null) end = null;
        return saveVal;
    }
    public T RemoveLast()
    {
        if (start == null) return default(T);
        T saveVal = end.data;
        end.previous.next = start.previous;
        end = end.previous;
        if (start == null) end = null;
        return saveVal;
    }

    public void PrintAll()
    {
        Node<T> tmp = start;
        while (tmp != null)
        {
            Console.WriteLine(tmp.data);
            tmp = tmp.next;
        }
    }
    class Node<T>
    {
        public T data;
        public Node<T> next;
        public Node<T> previous;
        //etc
        public Node(T newData)
        {
            data = newData;
            next = null;
            previous = null;
        }
    }
}

几乎所有方法中都很少有连接问题。我已经修改了相同的内容,它工作正常。

public class DoubleLinkedList <T>
{
    Node<T> start;
    Node<T> end;
    public void AddFirst(T dataToAdd)
    {
        Node<T> tmp = new Node<T>(dataToAdd);
        if (start == null)
        {
            start = tmp;
            end = start;
            return;
        }
        start.previous = tmp;
        tmp.next = start;
        start = tmp;

        if (start.next == null)
        {
            end = start;
        }
    }
    public void AddLast(T dataToAdd)
    {
        if (start == null)
        {
            AddFirst(dataToAdd);
            return;
        }
        Node<T> tmp = new Node<T>(dataToAdd);
        end.next = tmp;
        tmp.previous = end;
        end = tmp;
    }
    public T RemoveFirst()
    {
        if (start == null) return default(T);
        T saveVal = start.data;
        start = start.next;
        start.previous = null;
        if (start == null) end = null;
        return saveVal;
    }
    public T RemoveLast()
    {
        if (start == null) return default(T);
        T saveVal = end.data;
        end = end.previous;
        end.next = null;
        if (start == null) end = null;
        return saveVal;
    }

    public void PrintAll()
    {
        Node<T> tmp = start;
        while (tmp != null)
        {
            Console.WriteLine(tmp.data.ToString());
            tmp = tmp.next;
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新