CSharp examples for Data Structure Algorithm:Linked List
ListNode, List and EmptyListException class declarations.
using System;// ww w .j a v a 2 s.c o m class ListNode { // automatic read-only property Data public object Data { get; private set; } // automatic property Next public ListNode Next { get; set; } public ListNode(object dataValue) : this(dataValue, null) { } public ListNode(object dataValue, ListNode nextNode) { Data = dataValue; Next = nextNode; } } public class List { private ListNode firstNode; private ListNode lastNode; private string name; // string like "list" to display public List(string listName) { name = listName; firstNode = lastNode = null; } public List() : this("list") { } public void InsertAtFront(object insertItem) { if (IsEmpty()) { firstNode = lastNode = new ListNode(insertItem); } else { firstNode = new ListNode(insertItem, firstNode); } } public void InsertAtBack(object insertItem) { if (IsEmpty()) { firstNode = lastNode = new ListNode(insertItem); } else { lastNode = lastNode.Next = new ListNode(insertItem); } } public object RemoveFromFront() { if (IsEmpty()) { throw new EmptyListException(name); } object removeItem = firstNode.Data; // retrieve data // reset firstNode and lastNode references if (firstNode == lastNode) { firstNode = lastNode = null; } else { firstNode = firstNode.Next; } return removeItem; // return removed data } public object RemoveFromBack() { if (IsEmpty()) { throw new EmptyListException(name); } object removeItem = lastNode.Data; // retrieve data // reset firstNode and lastNode references if (firstNode == lastNode) { firstNode = lastNode = null; } else { ListNode current = firstNode; // loop while current.Next is not lastNode while (current.Next != lastNode) { current = current.Next; // move to next node } // current is new lastNode lastNode = current; current.Next = null; } return removeItem; // return removed data } public bool IsEmpty() { return firstNode == null; } public void Display() { if (IsEmpty()) { Console.WriteLine($"Empty {name}"); } else { Console.Write($"The {name} is: "); ListNode current = firstNode; // output current node data while not at end of list while (current != null) { Console.Write($"{current.Data} "); current = current.Next; } Console.WriteLine("\n"); } } } public class EmptyListException : Exception { public EmptyListException() : base("The list is empty") { } public EmptyListException(string name): base($"The {name} is empty") { } public EmptyListException(string exception, Exception inner) : base(exception, inner) { } } class ListTest { static void Main() { var list = new List(); bool aBoolean = true; char aCharacter = '$'; int anInteger = 3; string aString = "hello"; list.InsertAtFront(aBoolean); list.Display(); list.InsertAtFront(aCharacter); list.Display(); list.InsertAtBack(anInteger); list.Display(); list.InsertAtBack(aString); list.Display(); // remove data from list and display after each removal try { object removedObject = list.RemoveFromFront(); Console.WriteLine($"{removedObject} removed"); list.Display(); removedObject = list.RemoveFromFront(); Console.WriteLine($"{removedObject} removed"); list.Display(); removedObject = list.RemoveFromBack(); Console.WriteLine($"{removedObject} removed"); list.Display(); removedObject = list.RemoveFromBack(); Console.WriteLine($"{removedObject} removed"); list.Display(); } catch (EmptyListException emptyListException) { Console.Error.WriteLine($"\n{emptyListException}"); } } }