This question is written in java. You are not allowed to use Java API classes fo
ID: 3786742 • Letter: T
Question
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!
Explanation / Answer
/* java program to implement dequeue*/
import java.io.*;
class dequeue
{
int A[] = new int[100];
int n;
int front,rear;
static BufferReader br = new BufferedReader(newInputStreamReader(System.in));
public dequeue(m)
{
n=m;
front=rear=0;
}
public void pushrear(int v)
{
if ((rear+1)<n)
{
rear++;
A[rear] = v;
}
else
System.out.println("Overflow from rear");
}
public void pushfront(int v){
if(front>=0)
{
A[front] = v;
front--;
}
else
System.out.println("Overflow from front");
}
public int popfront()
{
int v;
if ( front!= rear)
{
front++;
v= A[front];
return v;
}
else
return -9999;
}
public int poprear()
{
int v;
if(front!=rear)
{
v=A[rear];
rear--;
return v;
}
else
return -9999;
}
public void display()
{
if(front!=rear)
{
for(int k=front+1; k<=rear; k++)
System.out.println(" current dequeue is:" + A[k]);
}
else
System.out.print("no elements in the queue for nothing to eject or insert);
}
public static void main() throws IOException
{
System.out.print("enter the size of the queue: ");
int length = Integer.parseInt(br.readLine());
dequeue call = new dequeue (length);
int choice;
boolean exit = false;
while(!exit)
{
System.out.print(" 1:push 2: pop 3:inject 4: eject : exit Enter operation for dequeue: ");
choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1 ;
System.out.print(" Enter element to push");
int number1 = Integer.parseInt(br.readLine());
call.pushfront(number1);
call.display();
break;
case 2 ;
int popped1 = call popfront();
if(popped1!= -9999)
System.out.println(" deleted : " + popped1);
break;
case 3 ;
System.out.print(" Enter element to inject");
int number2 = Integer.parseInt(br.readLine());
call.pushfront(number2);
call.display();
break;
case 4 :
int popped2 = call popfront();
if(popped2!= -9999)
System.out.println(" deleted : " + popped2);
break;
case 5 :
exit = true;
break;
default:
System.out.println(" wrong choice please choose the above");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.