These are the instructions for the Deque application: Write an application class
ID: 3630764 • Letter: T
Question
These are the instructions for the Deque application:
Write an application class to test all of the methods defined in the MyDeque class. The application should allow the user to decide the length of the deque and operate on the queue as many times as he/she wants until he/she selects ‘Quit’.
The selections for users are:
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
You may use the following steps to test the functions:
a) Insert 4 elements to rear
b) Remove 2 elements from front
c) Insert 3 elements to rear
d) Remove 3 elements from rear
e) Insert 3 elements to front
This is my Deque Program:
public class MyDeque
{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
public MyDeque(int s)
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
public void insertFront(long j)
{
if (front == 0)
front = maxSize;
queArray[--front]= j;
nItems++;
}
public void insertRear(long j)
{
if (rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
nItems++;
}
public long removeFront()
{
long temp = queArray[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public long removeRear()
{
long temp = queArray[rear--];
if(rear == -1)
rear = nItems - 1;
nItems--;
return temp;
}
public long peekFront()
{
return queArray[front];
}
public long peekRear()
{
return queArray[rear];
}
public boolean isEmpty()
{
return (nItems == 0);
}
public boolean isFull()
{
return (nItems == maxSize);
}
public int size()
{
return nItems;
}
public String toString()
{
String s = "";
if(isEmpty())
{
s = "F [ Empty ] R";
}
else
{
s = "F[";
int i = front;
int j = rear;
while(i!=j)
{
s += queArray[i] + ",";
i++;
}
s += queArray[i] + "]R";
}
return s;
}
}
Explanation / Answer
public class DequeTest {
/**
* @param args
*/
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
System.out.println("Enter size:");
int size=input.nextInt();
int choice;
long ele;
MyDeque q=new MyDeque(5);
do
{
System.out.println(" 1) Insert to front");
System.out.println("2) Insert to rear");
System.out.println("3) Remove from front");
System.out.println("4) Remove from rear");
System.out.println("5) Peek front");
System.out.println("6) Peek rear 7) Display queue");
System.out.println("8) Quit");
System.out.println("Enter choice");
choice=input.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter element to insert:");
ele=input.nextLong();
q.insertFront(ele);
break;
case 2:
System.out.print("Enter element to insert:");
ele=input.nextLong();
q.insertRear(ele);
break;
case 3:System.out.print("Element removed:"+q.removeFront());
break;
case 4:System.out.print("Element removed:"+q.removeRear());
break;
case 5:System.out.print("Peak Front element"+q.peekFront());
break;
case 6:System.out.print("Peak rear element"+q.peekRear());
break;
case 7: System.out.print("Dequeue values:"+q);
break;
case 8:System.exit(0);
break;
}
}while(choice<8);
}
}
Enter size:
10
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:1
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:2
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:3
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:4
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
3
Element removed:1
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
3
Element removed:2
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
7
Dequeue values:F[3,4]R
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:5
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:6
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:7
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
4
Element removed:7
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
4
Element removed:6
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
4
Element removed:4
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
7
Dequeue values:F[3]R
1) Insert to front
2) Insert to rear
3) Remove from front
4) Remove from rear
5) Peek front
6) Peek rear
7) Display queue
8) Quit
Enter choice
2
Enter element to insert:9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.