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