Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so I was searching for simple linked list implementation in C# and found one
but I could not understand a line in that code.

public Node next;
what does a "next" variable declared as Node means
since Node here is a class, I am confusing it with object creation.

Here is the code.

A Linked List, at its core is a bunch of Nodes linked together.

So, you need to start with a simple Node class:

public class Node {
    public Node next;
    public Object data;
}
Then your linked list will have as a member one node representing the head (start) of the list:

public class LinkedList {
    private Node head;
}
Then you need to add functionality to the list by adding methods. They usually involve some sort of traversal along all of the nodes.

public void printAllNodes() {
    Node current = head;
    while (current != null) 
    {
        Console.WriteLine(current.data);
        current = current.next;
    }
}
Also, inserting new data is another common operation:

public void Add(Object data) {
    Node toAdd = new Node();
    toAdd.data = data;
    Node current = head;
    // traverse all nodes (see the print all nodes method for an example)
    current.next = toAdd;
}


What I have tried:

how I am confused. Node next=new Node();
but its not possible to create a instance in the same class itself right!
Posted
Updated 10-May-21 0:08am
v3

It is just a reference to the next Node in the linked list. Look at the Add method to see how it is implemented.
 
Share this answer
 
v2
public means the variable is available outside the class to anyone
Node is the type of the variable
next is it's name.

It means that if you have a sequence of Node instances in a list, you can traverse them all using the next variable of each:
C#
Node current = GetHead();
while (current != null)
   {
   DoSomethingWithTheNode(current);
   current = current.next;
   }


But ... there are quite a few bad practices there.
Firstly, having public fields is a bad idea as it means external code can access it at any time - and your class doesn't know anything about it. That means that you can't change how your Node class works in any way without thinking long and hard about the possible effects on external code - any change you make could break the external app either directly by stopping it compiling, or indirectly by changing how it works.
public shoudl be kept for properties, not applied to fields.

Secondly, next is a bad name - starting with a lowercase letter implies a local or internal variable, public items should start with an initial capital.

Thirdly, it's a very good idea to give an initial value to variables to prevent the compiler flagging up "uninitialized variable" errors:
C#
public Node Next {get; set;} = null;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900