/// <summary> /// Sorts a stack and stores sorted one in another temp stack /// </summary> public void Sort() { while (!s1.IsEmpty()) { int d = (int)s1.Pop(); if (s2.IsEmpty()) { s2.Push(d); } else { while (!s2.IsEmpty() && (int)s2.Peek() > d) { s1.Push(s2.Pop()); } s2.Push(d); } } Console.WriteLine("Sorted Stack: "); while (!s2.IsEmpty()) { Console.WriteLine(s2.Pop()); } }
static void Main() { LinkedListStack stack = new LinkedListStack(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); stack.Push(5); Console.WriteLine("Stack Pop: {0}", stack.Pop()); Console.WriteLine("Stack Pop: {0}", stack.Pop()); Console.WriteLine("Stack Pop: {0}", stack.Pop()); Console.WriteLine("Stack Peek: {0}", stack.Peek()); Console.WriteLine("Stack Pop: {0}", stack.Pop()); stack.Push(6); Console.WriteLine("Stack Pop: {0}", stack.Pop()); Console.ReadLine(); }
/// <summary> /// Removes an element from the queue /// <returns>Element to be removed from queue as per FIFO</returns> /// </summary> public object Dequeue() { if (s2.IsEmpty()) { while (!s1.IsEmpty()) { object d = s1.Pop(); s2.Push(d); } } return(s2.Pop()); }