Question
You are required to adopt an existing generic LinkedList to behave as a generic stack and a generic queue, called MyStack and MyQueue respectively. a)
You are required to adopt an existing generic LinkedList to behave as a generic stack and a generic queue, called MyStack and MyQueue respectively.
a) Create a generic MyStack class that uses generic LinkedList as the underlying data structure. Do not use the Stack class from the library. As you have learned, the stack is a LIFO (Last In First Out) collection. The Stacks have three main methods that youll need to add to MyStack
Push Add an item to the top of the stack
Pop Removes an item from the top of the stack and return the value
Peek Returns the value of the top item in the stack.
b) Create a generic MyQueue class that uses generic LinkedList as the underlying data structure.
Do not use the built-in Queue classes. Add the three-main method
Enqueue Adds an item as the last item in the generic LinkedList
Dequeue Removes the first item in the generic LinkedList and returns the value
Peek Returns the value of the first item in the linked list.
c) Implement IEnumerable interface for MyStack and MyQueue
Use the following tester:
class Program {
static void Main(string[] args) {
MyStack myStack = new MyStack();
MyQueue myQueue = new MyQueue();
myStack.Push("first"); myStack.Push("second");
myStack.Push("Third"); myStack.Push("fourth");
myStack.Push("fifth"); myStack.Push("sixth");
Console.WriteLine("Next Item to be Popped : "+ myStack.Peek());
Console.WriteLine(myStack.Pop()); Console.WriteLine("Next Item to be Popped : " + myStack.Peek());
Console.WriteLine("Stack Items");
foreach (var item in myStack) { Console.WriteLine(item); }
myQueue.Enqueue("first"); myQueue.Enqueue("second");
myQueue.Enqueue("Third"); myQueue.Enqueue("fourth");
myQueue.Enqueue("fifth"); myQueue.Enqueue("sixth");
Console.WriteLine(" Next Item to be dequeued : "+ myQueue.Peek());
Console.WriteLine(myQueue.Dequeue());
Console.WriteLine("Next Item to be dequeued : " + myQueue.Peek());
Console.WriteLine("Queue Items");
foreach (var item in myQueue) { Console.WriteLine(item);
} } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started