Question: we will implement a class, called QStack . Essentially, it is a stack, which stores Integer type of data. Note that two queues can implement

we will implement a class, called QStack. Essentially, it is a stack, which stores Integer type of data. Note that two queues can implement a stack. You are given two queues, q1 and q2. The QStack class will use them to store the data. The QStack class should provide the following methods:

1. A constructor QStack(), which creates an empty stack 2. Method size(), which returns the number of elements in a stack 3. Method push(Integer x), which adds a data on to the stack 4. Method pop(), which first returns the value of the item on the top of the

stack, and then, removes the item from the stack 5. Method peek(), which returns the value of the item on the top of the stack 6. Method empty(), which returns true if and only if it is an empty stack; false

otherwise.

Besides the QStack class, you also need to implement a TestProgram class, and put amain() method in it. In the main() method, you need to design some testing cases to show that all the required methods work correctly.

import java.util.ArrayDeque;

import java.util.LinkedList;

import java.util.Queue;

public class QStack {

Queue q1 = new ArrayDeque();

Queue q2 = new LinkedList();

public QStack(){

}

public int size(){

}

public void push(Integer x){

}

public int pop(){

}

public int peek(){

}

public boolean empty(){

}

}

public class TestProgram {

public static void main(String[] args) {

}

}

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!