Question

In: Computer Science

using java write a program As the title described, you should only use two stacks to...

using java write a program

As the title described, you should only use two stacks to implement a queue's actions. DO NOT use any other data structure and push, pop and top should be O(1) by AVERAGE.
The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.
Both pop and top methods should return the value of first element

example

push(1)
pop() // return 1 push(2)
push(3)
top() // return 2 pop() // return 2

Solutions

Expert Solution

Here the concept behind using the queue using two stacks is that this method makes sure that oldest entered element is always at the top of stack 1, so that deQueue operation just pops from stack1. To put the element at top of stack1, another stack tmp is used.

q_push is the push method of the queue. q_pop is the pop method of the queue. q_top is the top method of the queue. Here more time is used while doing the push operation while compared to the pop and top method. Both of the latter operation use O(1) time fo computing .

class queueUsingStack
{
Stack<Integer> s ;
Stack<Integer> tmp ;
public queueUsingStack()
{
s = new Stack<Integer>();
tmp = new Stack<Integer>();
}
public void q_push(int data)
{   
if (s.size() == 0)
s.push(data);
else
{   
int l = s.size();
for (int i = 0; i < l; i++)
tmp.push(s.pop());   
s.push(data);
for (int i = 0; i < l; i++)
s.push(tmp.pop());   
}
}
public int q_pop()
{
if (s.size() == 0)
throw new NoSuchElementException("Underflow Exception");
return s.pop();
}
public int q_top()
{
if (s.size() == 0)
throw new NoSuchElementException("Underflow Exception");
return s.peek();
}
public boolean isEmpty()
{
return s.size() == 0 ;
}
public int getSize()
{
return s.size();
}
}


Related Solutions

Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows:...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows: There is a MAZE. There is a mouse inside the maze. The mouse must find the exit from the maze. When the user clicks the letter C or c a CAT is added to the maze. The cat will run through the maze and if it finds the mouse it should eat it and the game is over! User can add as many cats...
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Write a java program (use value returning method) that gives simple math quizzes. The program should...
Write a java program (use value returning method) that gives simple math quizzes. The program should display two random integers from 1 to 100 that are to be added, such as:    47 + 29 The program allows the user to enter the answer. If the answer is correct, display “congratulations”. If the answer is incorrect, the program should show the correct answer. Your result should look like, for example:    47 + 29 Enter your answer: 1 Wrong, the right answer...
write a java program that allows the user to insert two number consists of only 3...
write a java program that allows the user to insert two number consists of only 3 digits and print it and all a number between it but don't print any number that has digits equal to a number 4. like (211,212,213,215,...,350), if the user print number that not equal to 3 digits like ( 23,2298,1) print invalid number
We are trying to use two stacks to implement a queue. Name the two stacks as...
We are trying to use two stacks to implement a queue. Name the two stacks as E and D. We will enqueue into E and dequeue from D. To implement enqueue(e), simply call E.push(e). To implement dequeue(), simply call D.pop(), provided that D is not empty. If D is empty, iteratively pop every element from E and push it onto D, until E is empty, and then call D.pop(). Considering the worst case running time, what is the performance in...
using java For this assignment, you will write a program that guesses a number chosen by...
using java For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to choose a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT