private static void TestMyPQStack() { MyPQStack <int> stack = new MyPQStack <int>(); // test empty stack Console.WriteLine(stack.Size() == 0); // test push and peek stack.Push(5); Console.WriteLine(stack.Size() == 1); Console.WriteLine(stack.Peek() == 5); stack.Push(2); Console.WriteLine(stack.Size() == 2); Console.WriteLine(stack.Peek() == 2); stack.Push(9); Console.WriteLine(stack.Size() == 3); Console.WriteLine(stack.Peek() == 9); // duplicate items not ignored stack.Push(3); Console.WriteLine(stack.Size() == 4); Console.WriteLine(stack.Peek() == 3); stack.Push(5); Console.WriteLine(stack.Size() == 5); Console.WriteLine(stack.Peek() == 5); // test pop int[] expected = { 5, 3, 9, 2, 5 }; for (int i = 0, size = stack.Size(); i < size; i++) { Console.WriteLine(stack.Pop().Equals(expected[i])); } }
/* Destructively transfer stack1 items to stack2 in reverse order */ private void FlipStack(MyPQStack <T> stack1, MyPQStack <T> stack2) { for (int i = 0, numItems = stack1.Size(); i < numItems; i++) { stack2.Push(stack1.Pop()); } }
public T Peek() { MyPQStack <T> temp = new MyPQStack <T>(); FlipStack(stack, temp); T peeked = temp.Peek(); FlipStack(temp, stack); return(peeked); }
public T Dequeue() { MyPQStack <T> temp = new MyPQStack <T>(); FlipStack(stack, temp); T dequeued = temp.Pop(); FlipStack(temp, stack); size--; return(dequeued); }
public void Clear() { stack = new MyPQStack <T>(); size = 0; }