Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[JAVA DATA STRUCTURES] A Queue object can be implemented using two Stack objects. Below is an outline of how to do this. Complete the implementations

[JAVA DATA STRUCTURES]

A Queue object can be implemented using two Stack objects. Below is an outline of how to do this. Complete the implementations of the add() and remove() methods. The key idea is that if you push all the elements from one stack onto an empty stack, then the items get reversed and what was the top-of-stack on the original stack becomes the bottom of the new stack. In the implementation below, one of the stacks is used to implement the add() method and the other stack is for the remove() method. It should always be the case that one of the two stacks is empty and the contents of the queue are in the other stack. By shifting the elements from one stack to the other, you can move the front of the queue to the top of a stack, or you can move the rear of the queue to the top of a stack.

import java.util.Stack;

public class Queue

{

private Stack forAdding = new Stack();

private Stack forRemoving = new Stack();

public void add(T item)

{

}

public T remove( )

{

}

}

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_2

Step: 3

blur-text-image_3

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

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

Is the page easy to enter, navigate, and exit? (608)

Answered: 1 week ago

Question

LO13.1 List the characteristics of monopolistic competition.

Answered: 1 week ago