CSharp examples for Data Structure Algorithm:Stack
Implementing a stack by inheriting from class List.
using System;/*from w w w.j a v a 2 s . c o m*/ public class StackInheritance : List { // pass name "stack" to List constructor public StackInheritance() : base("stack") { } // place dataValue at top of stack by inserting dataValue at front of linked list public void Push(object dataValue) { InsertAtFront(dataValue); } // remove item from top of stack by removing item at front of linked list public object Pop() { return RemoveFromFront(); } } class MainClass { static void Main() { StackInheritance stack = new StackInheritance(); // create objects to store in the stack bool aBoolean = true; char aCharacter = '$'; int anInteger = 3; string aString = "hello"; // use method Push to add items to stack stack.Push(aBoolean); stack.Display(); stack.Push(aCharacter); stack.Display(); stack.Push(anInteger); stack.Display(); stack.Push(aString); stack.Display(); try { while (true) { object removedObject = stack.Pop(); Console.WriteLine($"{removedObject} popped"); stack.Display(); } } catch (EmptyListException emptyListException) { // if exception occurs, write stack trace Console.Error.WriteLine(emptyListException.StackTrace); } } } 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) { } }