Question: Problem: Implement Queue ADT algorithm using *TWO* stacks. inStack - the stack to take elements that are pushed into the queue. outStack - the stack

Problem: Implement Queue ADT algorithm using *TWO* stacks. inStack - the stack to take elements that are pushed into the queue. outStack - the stack to pop the elements out of the queue. Both queues are empty at the beginning. Note, due to this the runtime will not be constant for some functions. Basic Functions: Push: inStack.push() Pop: If the queue is empty, return null. If outStack is empty Pop out all elements from inStack and push them into outStack Return outStack.pop(). Peek: If queue is empty, return null. If outStack is empty Pop out all elements from inStack and push them into outStack. Return outStack.peek() isEmpty: return inStack.isEmpty() && outStack.isEmpty() Size: return inStack.size() + outStack.size() How do I implement the two stacks using the basic functions? I am stumped. Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!