Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

What are the relative advantages and disadvantages of observation?

Answered: 1 week ago