In this practice, we will be implementing a queue using TWOSTACK object.
- You are required to use python.
- For help you can use the lecture slides.
Write a class called TWOSTACK that uses python list to implement two stacks sharing the same python list. The class has following methods. Implement each of these methods.
a. Push1(key)
Adds a new value to the stack1. Returns True on success else returns false.
b. Push2(key)
Adds a new value to the stack2. Returns True on success else returns false.
c. Pop1()
Returns the last value added to the stack1 and removes the value from the stack1.
d. Pop2()
Returns the last value added to the stack2 and removes the value from the stack2.
e. Peek1()
Returns the last value added to the stack1 and does not removes the value from the stack1.
f. Peek2()
Returns the last value added to the stack2 and does not removes the value from the stack2.
g. isFull1()
Returns true if the stack1 is full, false otherwise.
h. isFull2()
Returns true if the stack2 is full, false otherwise.
i. isEmpty1()
Returns true if the stack1 is empty, false otherwise.
j. isEmpty2()
Returns true if the stack2 is empty, false otherwise.
The init
method for the TWOSTACK class should take two parameters size1 and size2 denoting the maximum size of each stack (apart from the self parameter).
Write a class called QUEUE that uses instance of TWOSTACK class. The class also implements the following methods.
a. Enqueue(key)
Adds an item at the end of the queue.
b. Dequeue()
Returns the item at the beginning of the queue.
c. isFull()
Returns true if the queue is full, false otherwise.
d. isEmpty()
Returns true if the queue is empty, false otherwise.
During initialization, of the queue we will also pass a parameter to set the maximum queue size. This should be distributed equally between the two stacks. In your driver code check if this number is even.
Write a driver code to test your implementation and show that it is working for different scenarios.