Question: In Java Problem:Implement a queue with two stacks. The class for queues is declared in C++ as below. Please implement two functions: appendTail to append
In Java



Problem:Implement a queue with two stacks. The class for queues is declared in C++ as below. Please implement two functions: appendTail to append an element into tail of a queue, and deleteHead to delete an element from head of a queue. Analysis:According to declaration above, a queue contains two stacks stack1 and stack2. Therefore, it is required to implement a queue which follows the rule "First In First Out" with two stacks which follow the rule of "First In Last Out" We analyze the process to add and delete some elements via some examples. Firstly en element a is inserted. Let us push it into stack1. There is an element (a) in stackland stack2 is empty. We continue to add two more elements b and c (push them into stack1 too). There are three elements (a, b, c} in stack! now, where c is on its top, and stack2 is still empty (as shown in Figure 1-a)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
