使双链表通用

时间:2018-07-28 13:37:35

标签: c#

我正在使用C#控制台应用程序。我正在尝试使双链表通用。

我上了这个课:

public class Node<T>
{
    public T data;
    public Node<T> next;
    public Node<T> previous;

    public Node(T d) => (data, next, previous) = (d, null, null);
    public void DisplayNode() => Console.Write("{" + data + "}");
}

和实际的双链接类:

public class DoubleLinkedList<T>
{
    private Node<T> _first;
    private Node<T> _last;
    // internal Node head;

    public DoubleLinkedList(Node<T> first, Node<T> last) => (_first, _last) = (first, last);
    public DoubleLinkedList() { }

    public bool InsertAfter(int key, T data)
    {
        Node<T> current = _first;

        while (current.data != key)
        {
            current = current.next;
            if (current == null)
                return false;
        }

        Node<T> newNode = new Node<T>(data);
        newNode.data = data;

        if (current == _last) (current.next, _last) = (null, newNode);
        else 
        {
            newNode.next = current.next;
            current.next.previous = newNode;
        }

        newNode.previous = current;
        current.next = newNode;

        return true;
    }

但是我在这条线上出现错误:

while (current.data != key)

说:

  

CS0019 C#运算符'!='不能应用于类型为'T'的操作数,并且   'T'

所以我的问题是:如何解决此错误?

谢谢

0 个答案:

没有答案