C# - Linked List implementation


Linked List implementation in C#

CODE
using System; class Program { public static void Main(string[] args) { LinkedList l = new LinkedList(); l.Add(0); l.Add(10); l.Add(20); l.Add(30); l.Add(40); l.Add(50); l.PrintElements(); l.Add(35, 4); l.PrintElements(); l.Replace(0, 1); l.PrintElements(); Console.ReadKey(); } } //The Node class to store the data and a reference to the next Node class Node { public int Data; public Node NextNode; public Node(int data) { Data = data; NextNode = null; } } class LinkedList { //Head points to the 1st Node in the List. //If the List is empty, it will be null public Node Head = null; //Method to add data at the end of the list public void Add(int data) { Node newNode = new Node(data); if (IsEmpty()) this.Head = newNode; else { Node currentNode = this.Head; while (currentNode.NextNode != null) { currentNode = currentNode.NextNode; } currentNode.NextNode = newNode; } Console.WriteLine("The element {0} is added. ", data); } //Method to add data at the specified index //If the index is out of range, it will be added at the end of the list public void Add(int data, int index) { Node newNode = new Node(data); if (IsEmpty() || index == 0) { newNode.NextNode = this.Head; this.Head = newNode; } else { Node currentNode = this.Head; int currentIndex = 0; while (currentNode.NextNode != null) { if (currentIndex == index-1) break; currentNode = currentNode.NextNode; currentIndex++; } Node tempNode = currentNode.NextNode; currentNode.NextNode = newNode; newNode.NextNode = tempNode; } Console.WriteLine("The element {0} is added. ", data); } //Method to display the element at a specified index public void Peek(int index) { int i = 0; Node currentNode = this.Head; bool isFound = false; while (currentNode != null) { if(i == index) { Console.WriteLine("Peek {0}: Element at index {0} : {1}", index, currentNode.Data); isFound = true; break; } currentNode = currentNode.NextNode; i++; } if (!isFound) Console.WriteLine("Peek {0}: The index {0} is not valid.", index); } //Method to delete a Node if the data equals the data provided //It will return a messsage if the Node with the given data is not found public void Delete(int data) { if (IsEmpty()) Console.WriteLine("The List is empty."); else if (this.Head.Data == data) { this.Head = this.Head.NextNode; Console.WriteLine("The node with the value {0} is deleted.", data); } else { Node currentNode = this.Head; while (currentNode.NextNode != null) { if (currentNode.NextNode.Data == data) break; currentNode = currentNode.NextNode; } if (currentNode.NextNode == null) Console.WriteLine("A node with the value {0} does not exist.", data); else { currentNode.NextNode = currentNode.NextNode.NextNode; Console.WriteLine("The node with the value {0} is deleted.", data); } } } //Method to delete a Node at a specified index //It will return a messsage if the index is not valid public void DeleteAtIndex(int index) { bool isDeleted = false; if (IsEmpty()) Console.WriteLine("The List is empty."); else if (index == 0) { this.Head = this.Head.NextNode; isDeleted = true; Console.WriteLine("The node at index {0} is deleted.", index); } else { Node currentNode = this.Head; int currentIndex = 0; while (currentNode.NextNode != null) { if (currentIndex == index-1) { currentNode.NextNode = currentNode.NextNode.NextNode; isDeleted = true; break; } currentNode = currentNode.NextNode; currentIndex++; } if (isDeleted) Console.WriteLine("The node at index {0} is deleted.", index); else Console.WriteLine("The index {0} is not valid.", index); } } //Method to replace the Node at the given index with a new data public void Replace(int index, int data) { int i = 0; Node currentNode = this.Head; bool isFound = false; while (currentNode != null) { if (i == index) { int oldData = currentNode.Data; currentNode.Data = data; Console.WriteLine("Element at index {0} with data {1} is replaced with {2}", index, oldData, currentNode.Data); isFound = true; break; } currentNode = currentNode.NextNode; i++; } if (!isFound) Console.WriteLine("The index {0} is not valid.", index); } //Method to print all the elements in the List public void PrintElements() { if (IsEmpty()) { Console.WriteLine("The Linked List is empty."); } else { Console.Write("Linked List: "); Node currentNode = this.Head; while (currentNode != null) { Console.Write(currentNode.Data); if (currentNode.NextNode != null) Console.Write(" ==> "); currentNode = currentNode.NextNode; } Console.WriteLine(""); } } //Method to check if the List is empty public bool IsEmpty() { if (this.Head == null) return true; else return false; } //Method to get the total number of Nodes in the List public int GetSize() { int size = 0; Node currentNode = this.Head; while (currentNode != null) { currentNode = currentNode.NextNode; size++; } return size; } }

INPUT & OUTPUT
The below is the output of the program.

Comments