1

I am creating linked list in c#, I have been using a website to learn from https://www.codeproject.com/Articles/1104980/Linked-List-Implementation-in-Csharp

This site has code that i try and I have issues with a problem. The code i am using is

public class Node
{
    public Node Next;
    public object Value;
}
public class LinkedList
{
    private Node head;
    private Node current;//This will have latest node
    public int Count;
}

public LinkedList()
{
    head = new Node();
    current = head;
}

public void AddAtLast(object data)
{
    Node newNode = new Node();
    newNode.Value = data;
    current.Next = newNode;
    current = newNode;
    Count++;
}

public void AddAtStart(object data)
{
    Node newNode = new Node() { Value = data};
    newNode.Next = head.Next;//new node will have reference of head's next reference
    head.Next = newNode;//and now head will refer to new node
    Count++;
}

The code copied from website, so should be no error. But my problem is, when i add items to the start of the list with AddAtStart and then i add at end of list with AddAtLast, AddAtLast deletes all the nodes i had added before and only now stores the new AddAtLast entry. I maby think this could be because of current. I think that current is thinking it is the head and not the last node, so when i add at end it adds at start and removes all. Could this be the cause of my problems.

If I only use AddAtStart it all works good, all nodes get added, i can have many nodes, but just when AddAtLast is used, everything goes away

EDIT i forgot my code was differet from site my apologies. I shall edit with my code I am using

public class LinkedList
{
    private Node head;
    private Node current;//This will have latest node
    public int Count;

    public LinkedList()
    {
        head = new Node();
        current = head;
    }

    public void PrintAllNodes()
    { 
        //Traverse from head
        Console.Write("Head ->");
        Node curr = head;
        while (curr.Next != null)
        {
            curr = curr.Next;
            Console.Write(curr.Value);
            Console.Write("->");
        }
        Console.Write("NULL");
    }

    public void AddAtStart(object data)
    {
        Node newNode = new Node() { Value = data};
        newNode.Next = head.Next;//new node will have reference of head's next reference
        head.Next = newNode;//and now head will refer to new node
        Count++;
    }

    public void AddAtLast(object data)
    {
        Node newNode = new Node();
        newNode.Value = data;
        current.Next = newNode;
        current = newNode;
        Count++;
    }
}
2
  • I added indentation to your code. I assume because you say it compiles and runs that you've made a mistake copying it here and the constructor for the LinkedList class as well as the AddAtLast() and AddAtStart() methods are meant to be in the LinkedList class? Commented Dec 2, 2018 at 3:42
  • yes it was mistake, i added the correct code as edit now Commented Dec 2, 2018 at 3:52

2 Answers 2

1

The issue is caused because the structure of your class LinkedList need to be fixed. You will have to move its constructor and methods into its declaration as below:

public class Node
{
    public Node Next;
    public object Value;
}

public class LinkedList
{
    private Node head;
    private Node current;//This will have latest node
    public int Count;

    public LinkedList()
    {
        head = new Node();
        current = head;
    }

    public void AddAtLast(object data)
    {
        Node newNode = new Node();
        newNode.Value = data;
        current.Next = newNode;
        current = newNode;
        Count++;
    }

    public void AddAtStart(object data)
    {
        Node newNode = new Node() { Value = data };
        newNode.Next = head.Next; //new node will have reference of head's next reference
        head.Next = newNode; //and now head will refer to new node
        Count++;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I forgot, i apologies I forgot I actually have it the way you said. I copied from site rather than my code to all my comments would not be copied over. But I have everything within the linkedlist class and i am getting my issue. My class linkedList is not public though, maby that issue. I will try that
0

Your current node is set to head in the constructor and then only updated in AddAtLast().
What is happening is when you add nodes with AddAtStart() current doesn't change and always points to head so when you call AddAtLast() it makes head.Next point to the newNode and then makes current the new node. You lose all the nodes that head used to point to.

To add to the end of a linked list, you have to traverse the list until you come to a node where node.next == null (i.e. the last node) then add the node there. Alternatively, create a field Node Last and update it to always point to the last node whenever you add to the end of the list. Then you can just append new nodes to Last.

The following will update current so it points to the last node then add a node to the end.

public void AddAtLast (object data) {
    Node newNode = new Node();
    newNode.Value = data;
    while (_current.Next != null) {
        current = current.Next; // Traverse current until it points to the last node
    }
    current.Next = newNode;
    current = newNode;
    Count++;
}

1 Comment

yes i thought current = head was maby the problem. Thank you for helping, I can fix it from here i hope. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.