Create class Stack by encapsulating List's capabilities - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Stack

Description

Create class Stack by encapsulating List's capabilities

using System;
public class MyStack
{
   private List stack;
   // construct empty stack
   public MyStack()
   {
      stack = new List("stack");
   }
   // add object to stack
   public void Push(object dataValue)
   {
      stack.InsertAtFront(dataValue);
   }
   // remove object from stack
   public object Pop()
   {
      return stack.RemoveFromFront();
   }
   // determine whether stack is empty
   public bool IsEmpty()
   {
      return stack.IsEmpty();
   }
   // output stack contents
   public void Display()
   {
      stack.Display();
   }
}
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) { }
}

Related Tutorials