In this article, I am going to discuss one of the most important Data Structures- Linked List.
Before proceeding further i would recommend to download source code from Github
The node of a singly linked list contains a data part and a link part. The link will contain the address of next node and is initialized to null. So, we will create class definition of node for singly linked list as follows –
internal class Node { internal int data; internal Node next; public Node(int d) { data = d; next = null; } }
The node for a Doubly Linked list will contain one data part and two link parts – previous link and next link. Hence, we create a class definition of a node for the doubly linked list as shown below.
internal class DNode { internal int data; internal DNode prev; internal DNode next; public DNode(int d) { data = d; prev = null; next = null; } }
Now, our node has been created, so, we will create a linked list class now. When a new Linked List is instantiated, it just has the head, which is Null.The SinglyLinkedList class will contain nodes of type Node class. Hence, SinglyLinkedList class definition will look like below.
internal class SingleLinkedList { internal Node head; }
The DoublyLinkedList class will contain nodes of type DNode class. Hence, DoublyLinkedList class will look like this.
internal class DoubleLinkedList { internal DNode head; }
internal void InsertFront(SingleLinkedList singlyList, int new_data) { Node new_node = new Node(new_data); new_node.next = singlyList.head; singlyList.head = new_node; }
To insert the data at front of the doubly linked list, we have to follow one extra step .i.e point the previous pointer of head node to the new node. So, the method will look like this.
internal void InsertFront(DoubleLinkedList doubleLinkedList, int data) { DNode newNode = new DNode(data); newNode.next = doubleLinkedList.head; newNode.prev = null; if (doubleLinkedList.head != null) { doubleLinkedList.head.prev = newNode; } doubleLinkedList.head = newNode; }
internal void InsertLast(SingleLinkedList singlyList, int new_data) { Node new_node = new Node(new_data); if (singlyList.head == null) { singlyList.head = new_node; return; } Node lastNode = GetLastNode(singlyList); lastNode.next = new_node; }
To insert the data at the end of a doubly linked list, we have to follow one extra step; .i.e., point previous pointer of new node to the last node.so the method will look like this.
internal void InsertLast(DoubleLinkedList doubleLinkedList, int data) { DNode newNode = new DNode(data); if (doubleLinkedList.head == null) { newNode.prev = null; doubleLinkedList.head = newNode; return; } DNode lastNode = GetLastNode(doubleLinkedList); lastNode.next = newNode; newNode.prev = lastNode; }
The last node will be the one with its next pointing to null. Hence we will traverse the list until we find the node with next as null and return that node as last node. Therefore the method to get the last node will be
internal Node GetLastNode(SingleLinkedList singlyList) { Node temp = singlyList.head; while (temp.next != null) { temp = temp.next; } return temp; }
internal void InsertAfter(Node prev_node, int new_data) { if (prev_node == null) { Console.WriteLine("The given previous node Cannot be null"); return; } Node new_node = new Node(new_data); new_node.next = prev_node.next; prev_node.next = new_node; }
internal void InsertAfter(DNode prev_node, int data) { if (prev_node == null) { Console.WriteLine("The given prevoius node cannot be null"); return; } DNode newNode = new DNode(data); newNode.next = prev_node.next; prev_node.next = newNode; newNode.prev = prev_node; if (newNode.next != null) { newNode.next.prev = newNode; } }
So, the method for singly linked list will look like this,
internal void DeleteNodebyKey(SingleLinkedList singlyList, int key) { Node temp = singlyList.head; Node prev = null; if (temp != null && temp.data == key) { singlyList.head = temp.next; return; } while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } if (temp == null) { return; } prev.next = temp.next; }
To perform this operation on doubly linked list we don’t need any extra pointer for previous node as Doubly linked list already have a pointer to previous node.so the delete method will be
internal void DeleteNodebyKey(DoubleLinkedList doubleLinkedList, int key) { DNode temp = doubleLinkedList.head; if (temp != null && temp.data == key) { doubleLinkedList.head = temp.next; doubleLinkedList.head.prev = null; return; } while (temp != null && temp.data != key) { temp = temp.next; } if (temp == null) { return; } if (temp.next != null) { temp.next.prev = temp.prev; } if (temp.prev != null) { temp.prev.next = temp.next; } }
This is one of the most famous interview questions. We need to reverse the links of each node to point to its previous node, and the last node should be the head node.This can be achieved by iterative as well as recursive methods. Here I am explaining the iterative method.
The method will look like this,
public void ReverseLinkedList(SingleLinkedList singlyList) { Node prev = null; Node current = singlyList.head; Node temp = null; while (current != null) { temp = current.next; current.next = prev; prev = current; current = temp; } singlyList.head = prev; }
We have implemented Singly Linked list and Doubly Linked list using C#. We have also performed various operations on them. Please refer to the attached code for better understanding. You will find a few more methods in the attached code such as finding middle element and searching a linked list.
Download the source code from Github
You can also find this article at C# Corner
You can find my other articles on data structures here.
Introduction Blazor is a .NET web framework that allows us to create client-side applications using…
Introduction In this article, we are going to create a sudoku solver with the help…
Introduction In this article, we will learn how to implement Azure serverless with Blazor web…
Introduction Angular is an open-source framework that allows us to create applications for multiple platforms…
Introduction In this article, we will create an optical character recognition (OCR) application using Angular…
Introduction In this article, we will create an optical character recognition (OCR) application using Blazor…
View Comments
Thank you for this article, it is very helpful! Do you have a solution for Removing "ALL" Nodes that match a given key value? I have been trying to implement a RemoveAll() by manipulating code given in (4) above, but keep getting a NULL error and/or infinite loop. :( .
Never mind, I figured it out. Woo Hoo! Thanks again, you are a great resource.
Hi ,
Thanks for reading my article. I am glad that you figure out the issue by yourself. If possible please share the code snippet here so that other people will also get befitted.