Explain the purpose of the program as detail as possible 3%. Develop a solution
ID: 3885114 • Letter: E
Question
Explain the purpose of the program as detail as possible 3%. Develop a solution for the problem and mention algorithm used -12% List data structures to be used in the solution. - 2%. Give a description of how to use the program and expected input/output - 3% For each method, give the pre and post conditions and invariants, if any - 5% Program execution according to the requirements 50% Naming of program as required 5% Useful comments and readability: 20% Write a program to implement a queue using stack. The file name should be MyQueue.java.Explanation / Answer
Purpose of the program:
Implement a queue data structure using stack data structure.
Solution:
In order to implement queue using stack we need to have two stacks to do the purpose of one queue.
Data Structures : Stack
Program:
import java.util.Scanner;
import java.util.Stack;
public class MyQueue
{
static class Queue
{
Stack<Integer> stack1 ;
Stack<Integer> stack2 ;
}
static void push(Stack<Integer> push_stk, int new_data)
{
push_stk.push(new_data);
}
static int pop(Stack<Integer> pop_stk)
{
if(pop_stk.isEmpty())
{
System.out.println("Stack Overflow");
System.exit(0);
}
return pop_stk.pop();
}
static void Enqueue(Queue que, int x)
{
push(que.stack1, x);
}
static int Dequeue(Queue que)
{
int x;
if(que.stack1.isEmpty() && que.stack2.isEmpty() )
{
System.out.println("Q is empty");
System.exit(0);
}
if(que.stack2.isEmpty())
{
while(!que.stack1.isEmpty())
{
x = pop(que.stack1);
push(que.stack2, x);
}
}
x = pop(que.stack2);
return x;
}
static void display(Queue myq)
{
int length1=myq.stack1.size();
int length2=myq.stack2.size();
if(length1+length2==0)
{
System.out.println("Queue is empty");
}
else
{
System.out.println("Queue Elements");
for(int i=0;i<length1;i++)
{
System.out.print(myq.stack1.get(i)+" ");
}
for(int i=length2-1;i>=0;i--)
{
System.out.print(myq.stack2.get(i)+" ");
}
System.out.println();
}
}
public static void main(String args[])
{
Queue myq= new Queue();
myq.stack1 = new Stack<>();
myq.stack2 = new Stack<>();
Scanner scan = new Scanner(System.in);
int choice;
do
{
System.out.println("1.Insert element to queue ");
System.out.println("2.Delete element from queue ");
System.out.println("3.Display all elements of queue ");
System.out.println("4.Quit ");
System.out.println("Enter your choice : ");
choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert into stack");
Enqueue( myq,scan.nextInt() );
break;
case 2 :
System.out.println("Removed Element = "+ Dequeue(myq) );
break;
case 3 :
display(myq);
break;
case 4 : System.out.println("Quiting");
System.exit(0);
break;
default: System.out.println("Wrong input");
break;
}
}while(choice>0 && choice<5);
}
}
Output:
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice :
1
Enter integer element to insert into stack
1
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice :
1
Enter integer element to insert into stack
2
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice :
1
Enter integer element to insert into stack
3
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice :
3
Queue Elements
1 2 3
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice :
2
Removed Element = 1
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice :
3
Queue Elements
2 3
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice :
4
Quiting
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.