Simple linked list implementation in C#

Linked list is the most basic data structure taught in the colleges.Though it looks simple,the kind of complex problems we can solve through it is just amazing.In languages like C,C++ its very easy to understand though pointers.Unfortunately or fortunately in C#,we don't need pointers to learn about linked list though internally,its all pointers all the way to the memory addresses.In this post I will try to explain how a link list can be implemented in C#.

Ok,enough of technical jargon.Lets come straight to the point.
Definition : A linked list is a simple chain of nodes where a node points to next node and so on till the next node doesn't points to anything.
Now what is a node? A node is a simple object which contains some data and a reference to next node.Lets code both our node and linked list class.




The Node class is self explanatory.
As we can see the Linked List class just contains a head node and all the nodes are added to this node.A little bit of explanation on Add function.It takes a parameter of Node type which we are adding to the head.
The main point to note down here is that we are not adding after the head,rather we are adding before the head.If we realise, we find that it is based on stack based adding which will become more evident when we print the nodes.

Lets write the client code.It is a simple console application which uses these 2 classes.
Following is the output :
fourth
third
second
first

And we are not surprised why this is the output.
Now lets modify our linkedlist class so that we always add a node after the head.The modified Add function is :


This time the output is :
first
second
third
fourth

This is just the beginning of linked lists and in future we will see more practical examples of linked lists.This is the beauty of C# that internal memory addressing is hidden from us and all we have to know is how reference and value types work in C#. .NET runtime do the all work of referencing and dereferencing for us.


Comments

  1. This is an excellent and simple example

    ReplyDelete
  2. ohhhh!!!!! Sir thanks Sir. Finally I able to program linked list in c#. very simple and great tutorial. Thanks

    ReplyDelete
  3. thanks for simple yet very valuable article

    ReplyDelete
  4. The print function is implemented in such a way that you can use it to print the contents of the linked list only once. In my opinion, it is better to have a function that does its job every time it is called.

    I would suggest adding a traversal pointer to the print function, and the problem is solved.

    public void PrintNodes()
    {
    Node traversal = head;
    while (traversal != null)
    {
    Console.WriteLine(traversal.data);
    traversal = traversal.next;
    }
    }

    ReplyDelete
  5. thank you so much!!!!! im really very grateful to u. this program is very clear with good explanation.i can now picture linked lists in my mind very clearly. i've been breaking my head from past 1 week not understanding linked list. this solved my problem.

    ReplyDelete
  6. It was very well explained. Thanks a lot.

    - Bharat

    ReplyDelete

Post a Comment

Popular posts from this blog

Some basic linked list problems

Composition over Inheritance