Linked List in C#

Introduction

 
In this blog, we are discussing the linked list concepts with simple programs.
 
The linked list is an abstract data type that represents the family of data structures where data structures represent a linear and sequential way.
  • Please check the below snapshot:
 
 
We can see in the above snapshot A, B, C, that these are nodes. A node has two elements, data, and pointer. A pointer points towards to the next node. A points towards B and B points towards C.
 
We have 3 types of linked lists:
  1. Simple linked list: We have a node and the node points towards the next node. Here we have a one-way pointer.
  2. Double linked list: Node points towards the next node, also it will point to the previous node.
  3. Circular linked list: Every node will point towards the next node and also the last node points to the first node. 
Let's create a simple linked list.
 
Step 1
 
First, create a simple console application.
 
Step 2
 
First, we need to create one node. Please create one class and name it as a node. This class has two properties, one is data and another one is the pointer. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace LinkedList  
  6. {  
  7.    public class Node  
  8.     {  
  9.         object data = null;  
  10.         Node next = null;  
  11.     }  
  12. }  
Step 3
 
We need to create one more class which holds the logic of the linked list concepts. Let's create AddFirst, AddLast, ReadAll methods.
 
In the AddFirst method, it will add data before the current head. First, we need to create the first item and we want this as our first item so we need to set items as the head.
  1. public void AddFirst(object data)  
  2.         {  
  3.             Node newitem = new Node();  
  4.             newitem.data = data;  
  5.             newitem.next = head;  
  6.             newitem = head;  
  7.         }   
In the AddLast method, we are creating the last node. Loop and find the last node and if it is null this means this is the last element to the current next set new item.
  1. public void  AddLast(object data)  
  2.        {  
  3.   
  4.            Node newitem = new Node();  
  5.            newitem.data = data;  
  6.            // if head is null that means it is the first item and  add the  new item and  set next node to null  
  7.            if (head ==null)  
  8.            {  
  9.                  
  10.                head = newitem;  
  11.                head.next = null;  
  12.            }  
  13.            else  
  14.            {  
  15.                // if the head is not null and find out the last node by loop  
  16.                Node current = head;  
  17.                while(current.next  != null)  
  18.                {  
  19.                    current = current.next;  
  20.                }  
  21.   
  22.                current.next = newitem;  
  23.            }  
  24.   
  25.   
  26.        }  
In the ReadAll method using a loop, we can read the data of the node. 
  1. public void ReadAll()  
  2. {  
  3.     Node current = head;  
  4.     // loop until the last node  
  5.     while(current.next !=null)  
  6.     {  
  7.         Console.WriteLine(current.data);  
  8.        current = current.next;  
  9.     }  
  10.   
  11.     Console.WriteLine(current.data);  
  12.     Console.ReadLine();  
  13.   
  14. }  
In your main method, create the object for class and run the application.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             LinkList linkedlistobject = new LinkList();  
  6.             linkedlistobject.AddLast("One");  
  7.             linkedlistobject.AddLast("Two");  
  8.             linkedlistobject.AddLast("Three");  
  9.             linkedlistobject.AddLast("Four");  
  10.             linkedlistobject.AddFirst("Add data to first data");  
  11.             linkedlistobject.ReadAll();  
  12.         }  
  13.     }  
Please check the below snapshot for results:
 
 
 
Summary
 
In this blog, we discussed a linked list and its types with a simple program.
 
Eat->Code->Sleep->Repeat.