1

I am working on a C# console application. I am trying to make the double linked list Generic.

I have this class:

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 + "}");
}

and the actual double linked class:

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;
    }

But I get an error on this line:

while (current.data != key)

saying:

CS0019 C# Operator '!=' cannot be applied to operands of type 'T' and 'T'

So my question is: How to solve this error?

Thank you

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
savantKing
  • 89
  • 1
  • 11

0 Answers0