Dolev
Dolev

Reputation: 151

C# double linked list - previous and next wont connect

i'm learning a bit about linked list and double linked list.

I tried to build a double linked list with previous and next nodes but when i debbug the program the "pointers" wont connect to each other in the places where they're supposed to. in the end of the run of the program the only reasult that is printed out is the last value i wanted to enter the list which was the ONLY value in the list apperantly.

this is my code, i belive it should be alright exept for the problam where the 'previous' and the 'next' nodes wont connect to each other.

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;

        }

    }
}

Upvotes: 0

Views: 366

Answers (1)

Deepak Tiwari
Deepak Tiwari

Reputation: 186

There were few connection problem in almost all the methods. I have modified the same and it is working fine.

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

Upvotes: 1

Related Questions