Implement the following operations of a queue using stacks p

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. . pop0 -- Removes the element from in front of queue. e peek) Get the front element. empty0 Return whether the queue is empty

Solution

Following program would fulfill the requirement:

import java.util.*; // import util package as we are going to use the classes and their attributes defined inside it.

//Now we will create a class which indicates a stack

class Stack

{

int array[];

int top, length,size;

public Stack(int l) //Constructor of the class which takes l as input which will be size of array

{

size = l;

lenght = 0;

array = new int[size]; //creating an array of size input.

top = -1;

}

public isEmpty() // function to check if queue is empty.

{

return top == -1;

}

//Function to get the front element of queue

public peek()

{

if (isEmpty()) //here we need to first check whether queue is empty or not and if empty then throw an error as we are looking for front element.

throw new NoSuchElementException(\"Underflow Exception\");

return array[top]; //returns the top element of stack i.e. as per requirement front element of queue.

}

//Now push function to add element to queue.

public push(int element)

{

if(top+1 > = size) //here we are checking that queue should not full is yes then throw error as we cann\'t add.

throw new IndexOutOfBoundException(\"Overflow exception\");

if(top+1 < size)

array[++top] = element;

length++;

}

//Pop function to remove element from front of the queue.

public pop()

{

if(isEmpty()) //First check if queue is empty

throw new NoSucElementFoundException(\"Underflow Exception\");

length--;

return[top--];

}

}

//Now we need a class to implement the above defined functions

class StackImplement

{

public static void main(String args[])

{ int i; //which takes input from user about the size of array

Scanner s = new Scanner(System.in);

System.out.println(\"Please enter the size of queue\");

i = s.nextInt();

Stack stack = new Stack(n);

//Now we can ask user what operation they want to perform on queue so switch case

char c;

System.out.println(\"Please choice one of the operation\");

System.out.println(\"1.) Push 2.) Pop 3.) Peek 4.) Empty\");

Scanner s = new Scanner(System.in);

int choice = s.nextInt();

switch(choice)

{

case 1:

System.out.println(\"Enter the element\");

try{ stack.push(s.nextInt());

}

catch(Exception e)

{System.out.println(e.getMessage());

}

break;

case 2:

try{

System.out.println(\"Popped element\" + stack.pop());

}

catch(Exception e)

{System.out.println(e.getMessage());}

break;

case 3 :

try{System.out.println(\"Peek Element\" + stack.peek());}

catch(Exception e) {System.out.println(e.getMessage());}

break;

case 4:

System.out.println(\"Empty status\" + stack.isEmpty());

break;

default:

System.out.println(\"Wrong choice\");

}

}

}

 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. . pop0 -- Removes the element from in front of queu
 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. . pop0 -- Removes the element from in front of queu
 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. . pop0 -- Removes the element from in front of queu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site