Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Documentation: 20% Explain the purpose of the program as detail as possible 3%.

ID: 3885249 • Letter: D

Question

Documentation: 20% 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% Programming: 80% 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% Description of program: Write a program to implement a queue using stack. The file name should be MyQueue.java.

Explanation / Answer

import java.util.Stack;
import java.util.Scanner;

public class HelloWorld
{
  
static class Queue //queue class having two class
{
Stack<Integer> st1 ;
Stack<Integer> st2 ;
}

  
static void push(Stack<Integer> tf, int na) //push function
{
  
tf.push(na); //pushing the data into the stack
}

  
static int pop(Stack<Integer> tf) //pop function
{
  
if(tf.isEmpty()) //checking if stack is empty
{
System.out.println("Stack Overflow");
System.exit(0);
}
  
return tf.pop(); //poping the data from the stack
}
  
static void enq(Queue s, int y) //enqueue function
{
push(s.st1, y);
}
  
static int deq(Queue s) //dequeue function
{
int y;
  
if(s.st1.isEmpty() && s.st2.isEmpty() ) //checking if stack
{ //empty
System.out.println("Queue is empty");
System.exit(0);
}
  
if(s.st2.isEmpty()) //relocation of element form stack 1 to 2
{   
while(!s.st1.isEmpty())
{
y = pop(s.st1);
push(s.st2, y);
  
}
}
y = pop(s.st2);
return y;
}

  
public static void main(String args[]) //main function
{
int n,i,num;
Scanner sc=new Scanner(System.in);
Queue s= new Queue(); //object of queueu
s.st1 = new Stack<>();
s.st2 = new Stack<>();
System.out.println("Enter the number of elemen "); //accepting number of elemnt from user
n=sc.nextInt();
System.out.println("Enter the elements"); //accepting the element
for(i=0;i<n;i++)
{
num=sc.nextInt();
enq(s,num); //pushing value into stack or enqueueing
}

//poping value from stack or dequeuing
System.out.println("Popping the elements");
for(i=0;i<n;i++)
{
System.out.print(deq(s)+" ");
  
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote