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
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