In this lab, you'll explore how to utilized existing collection types. Specifically, you'll develop a small console application that instantiates a Stack and Queue and then makes use of these data structures, inserting the user's input into each and displaying their contents.
The lab will have multiple steps, and you should try and perform the lab steps on your own, but certainly click through the how-to links to see step-by-step walkthroughs on how to complete each step.
Here are the steps:
The first step is to utilize/instantiate the stack and queue in your Main method. This is easily done like:
Stack<int> myStack = new Stack<int>();
Queue<int> myQueue = new Queue<int>();
Now loop until the user types -1, asking the user for a number and inserting this number into both the stack and the queue.
You'll want to use the Enqueue() method of the queue class and the Push() method of the stack class to insert the user's input.
Click this how-to to see a step-by-step solution for this part of the lab.
Now that you have the collections filled with the user's input, let's add DisplayContent methods to our program.
Write a method called DisplayContent that takes in a queue and prints out the content of the queue by repeatedly invoking the Dequeue() method.
Write another method (you can also call this DisplayContent or something else; this is permitted because of overloading) that takes in a stack and prints out the content of the stack by repeatedly invoking the Pop() method.
Since we are using Dequeue and Pop, the elements are actually being removed from the collections as we display them; if you didn't want this to happen, you would need to write this method slightly differently.
Then in Main, invoke the DisplayContent method for the stack and the queue. Notice the difference in the order in which the elements are accessed; this is as you should expect.
Click this how-to to see a step-by-step solution for this part of the lab.
Click her to see the finished ZIPed solution.