Question
Please use Java Thanks implement the queue using two stacks . One stack is used to enqueue elements, while the other is used to dequeue
Please use Java Thanks
implement the queue using two stacks. One stack is used to enqueue elements, while the other is used to dequeue elements. That is, when an element is added to the queue it is pushed on the in stack. When an element is removed from the queue it is popped off the out stack. If the out stack is empty, the contents of the in stack is transferred to the out stack.
What to do?
First, you need to implement the MyQueueI
Then you create the class TwoStackQueue
Your class should include two constructors:
A default constructor TwoStackQueue() to create an empty Queue by creating two empty stacks. Make use of the java.util.Stack
TwoStackQueue(List L) to create a Queue from a List (linkedList or ArrayList) by creating an empty out-stack and an in-stack filled with the List elements.
Implement the methods of the TwoStackQueue class by implementing all the abstract methods declared in the MyQueueI
You can use the push() and pop() methods from the Stack
In a main method, test your class thoroughly.
public interface MyQueueI
Interface to specify operations for a simple unbounded first-in-first-out (FIFO) queue.
Method Summary | |
public E | dequeue() Removes and returns the element at the front of this queue. |
public void | enqueue(E element) Adds the specified element to the back of this queue. |
public boolean | isEmpty() Returns true if this queue contains no elements. |
public E | peek() Returns, but does not remove, the element at the front of this queue. |
Coding: (Copy and Paste Source Code here)
public class UseTwoStackQueue {
public static void main(String[] args) {
List
listString.add("One");
listString.add("Two ");
listString.add("Three");
List
linkedString.add("end of linked list");
TwoStackQueue
TwoStackQueue
TwoStackQueue
TwoStackQueue
Q1.enqueue("First"); Q1.enqueue("Second"); Q1.enqueue("Third");
Q2.enqueue("Four");
System.out.println(" The element at the front of Q3" + Q3.peek());
Q4.enqueue(new Integer(200));Q4.enqueue(new Integer(100));
Q4.enqueue(new Integer(500)); Q4.enqueue(50);
System.out.println(" ******** Dequeue the Stack Q1");
while (!Q1.isEmpty()) System.out.print(" "+ Q1.dequeue());
System.out.println(" ******** The first element to dequeue in Q2");
System.out.print(" "+Q2.dequeue());
System.out.println(" ******** Dequeue the Stack Q3");
while (!Q3.isEmpty()) System.out.print(" "+ Q3.dequeue());
System.out.println(" ******** Dequeue the Stack Q4");
while (!Q4.isEmpty()) System.out.print(" "+ Q4.dequeue());
}
}
interface MyQueueI
// Complete and implement your interface here
class TwoStackQueue
// complete and implement your class here
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