This question is written in java You are not allowed to use

This question is written in java. You are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. The problem needs to work like the example below.

A deque is a data structure consisting of a list of items, on which the following operations are possible:

push(x): Insert item x on the front end of the deque.

pop(): Remove the front item from the deque and return it.

inject(x): Insert item x on the rear end of the deque.

eject(): Remove the rear item from the deque and return it.

Write a program in Java for the deque data structure. All the above operations should take O(1) time per operation. Your program should create a menu-driven interface as shown in the example below.

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 1

Enter element to push: 5

Current Deque: 5

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 3

Enter element to inject: 79

Current Deque: 5 79

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 3

Enter element to inject: 23

Current Deque: 5 79 23

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 1

Enter element to push: 16

Current Deque: 16 5 79 23

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 1

Enter element to push: 59

Current Deque: 59 16 5 79 23

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 2

Current Deque: 16 5 79 23

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 4

Current Deque: 16 5 79

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 4

Current Deque: 16 5

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 2

Current Deque: 5

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 4

Current Deque:

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 2

Deque is empty, nothing to pop.

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 4

Deque is empty, nothing to eject.

Enter operation for deque (1: Push, 2: Pop, 3: Inject, 4: Eject, 5: Quit): 5

Bye!

Solution

/*Java program to implement dequeue*/

import java.io.*;

class dequeue

{

int DQ[]=new int[100];

int n;

int front,rear;

static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public dequeue(int nn)

{n=nn;

front=rear=0;

}

public void pushrear(int v)

{

if(rear+1)<n)

{
rear++;

DQ[rear]=v;
}
else

System.out.println(“Overflow from rear!”);

}

public void pushfront(int v)

if(front>=0)
{

DQ[front]=v;
front--;
}
else

System.out.println(“Overflow from front!”);

}

public int popfront()

{

int v;


if(front!=rear)
{
front++;

v=DQ[front];return v;

}
else return -9999;


}

public int poprear()

{

int v;

if(front!=rear)
{

v=DQ[rear];
rear--;return v;

}
else return -9999;

}

public void display()

{ int i;

if(front!=rear)
{

for(i=front+1;i<=rear;i++)
System.out.println(DQ[i]);
}
else

System.out.println(\"Noelement\");
}
public static void main() throws IOException

{

System.out.print(“Enter size of the queue:”);

int size=Integer.parseInt(br.readLine());

dequeue call=new dequeue(size);

int choice;

boolean exit=false;

while(!exit)

{

System.out.print(\"\ 1:Push from Front\ ”);

System.out.print(“2:PushfromRear\ ”);

System.out.print(“3:Delete from Front\ 4:Delete from Rear\ 5:Display\ 6:Exit\ \ YourChoice:\");
choice=Integer.parseInt(br.readLine());
switch(choice)
{

case 1:

System.out.print(“\ EnterNumber”);

int num=Integer.parseInt(br.readLine())

call.pushfront(num);
break;

case 2:

System.out.print(\"\ EnterNumber\");

int num2=Integer.parseInt(br.readLine())

call.pushrear(num2);
break;

case 3:

int popped=call.popfront();

if(popped!=-9999)
System.out.println(\"\ Deleted\"+popped);
break;

case 4:

int popped2=call.popfront();

if(popped2!=-9999)
System.out.println(\"\ Deleted\"+popped2);
break;

case 5:

call.display();
break;

case 6:

exit=true;
break;
default:

System.out.println(\"\ WrongChoice!\");
break;
}
}
}
}

OUTPUT :
------

Enter size of the queue 6

1:PushfromFront
2:PushfromRear
3:DeletefromFront
4:DeletefromRear
5:Display
6:Exit

YourChoice:1
EnterNumber2
....
...
...
......
...

This question is written in java. You are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own
This question is written in java. You are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own
This question is written in java. You are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own
This question is written in java. You are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own
This question is written in java. You are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site