// QueueTest.cs: Demonstrating class Queue. using System; using System.Collections; public class QueueTest { public static void Main(string[] args) { Queue que = new Queue(); // create objects to store in the queue bool aBoolean = true; char aCharacter = '$'; int anInteger = 34567; string aString = "hello"; // use method Push to add items to (the end of) the queue que.Enqueue(aBoolean); PrintQueue(que); que.Enqueue(aCharacter); PrintQueue(que); que.Enqueue(anInteger); PrintQueue(que); que.Enqueue(aString); PrintQueue(que); // check the top element of the queue Console.WriteLine("The top element of the queue is {0}\n", que.Peek()); // remove items from stack try { while (true) { object removedObject = que.Dequeue(); Console.WriteLine(removedObject + " popped"); PrintQueue(que); } // end while } // end try catch (InvalidOperationException exception) { // if exception occurs, output stack trace Console.Error.WriteLine(exception); } // end catch } // end Main // display the contents of a stack private static void PrintQueue(Queue q) { if (q.Count == 0) { Console.WriteLine("queue is empty\n"); // the queue is empty } else { Console.Write("The queue is: "); // iterate through the queue with a foreach statement foreach (var element in q) { Console.Write("{0} ", element); // invokes ToString } Console.WriteLine("\n"); } // end else } // end method PrintQueue } // end class StackQueue