Complete the following code and create main method with given instructions: impo
ID: 647902 • Letter: C
Question
Complete the following code and create main method with given instructions:
import java.util.NoSuchElementException;
public class CircularQueue<E>
{
private E[] queue;
private int front = 0, rear = 0;
private static final int DEFAULT_CAPACITY = 5;
public CircularQueue(int capacity)
{
queue = (E[]) new Object[capacity + 1];
}
public CircularQueue()
{
this(DEFAULT_CAPACITY);
}
//Add a method that will determine if the queue is empty. Recall that the queue is empty if the front and
//rear index the same location in the array:
public boolean isEmpty()
{
return __________ == __________;
}
private boolean isFull()
{
return ((rear + 1) % queue.length) == front;
}
public void enqueue(E newElement)
{
if (isFull())
{
this.expand();
}
//To add an element to the rear of the queue, move the rear index up and add the element to the rear
//location in the queue:
rear = (rear + 1) % queue.length;
queue[__________] = newElement;
}
public E dequeue()
{
if(isEmpty())
{
throw new NoSuchElementException("Queue is empty");
}
front = (front + 1) % queue.length;
E frontElement = queue[front];
queue[front] = null;
return frontElement;
}
private void expand()
{
E[] newQueue = (E[]) new Object[(queue.length * 3) / 2 + 1];
int newIndex = 1;
while (!this.isEmpty())
{
// Dequeue all of the current elements
//add them to the new array beginning at index 1
newQueue[__________] = this.dequeue();
newIndex++;
}
//Set queue to reference the new array and update the front and rear pointers.
queue = newQueue;
front = _____;
rear = newIndex - 1;
}
}
Create a new program called QueueTest with a main method that performs the following steps:
1. Create a CircularQueue object with an initial capacity of 4.
2. Add the following String objects to the queue:
Explanation / Answer
#include<iostream.h>
#include<conio.h>
const int MAX = 5;
class cqueue
{
int a[MAX],front,rear;
public :
cqueue()
{
front=rear=-1;
}
void insert(int );
int deletion();
void display();
};
void cqueue :: insert(int val)
{
if((front==0 && rear==MAX-1) || (rear+1==front))
cout<<" Circular Queue is Full
";
else
{
if(rear==MAX-1)
rear=0;
else
rear++;
a[rear]=val;
}
if(front==-1)
front=0;
}
int cqueue :: deletion()
{
int k;
if(front==-1)
cout<<"Circular Queue is Empty
";
else
{
k=a[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
}
}
return k;
}
void cqueue :: display()
{
int i;
if(front==-1)
cout<<"Circular Queue is Empty
";
else
{
if(rear < front)
{
for(i=front;i<=MAX-1;i++)
cout<<a[i]<<" ";
for(i=0;i<=rear;i++)
cout<<a[i]<<" ";
}
else
{
for(i=front;i<=rear;i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
}
void main()
{
cqueue c1;
int ch,val;
char op;
do
{
clrscr();
cout<<"-----------Menu-------------
";
cout<<"1.Insertion
2.Deletion
3.Display
4.Exit
";
cout<<"Enter Your Choice <1..4> ?";
cin>>ch;
switch(ch)
{
case 1 : cout<<"Enter Element to Insert ?";
cin>>val;
c1.insert(val);
break;
case 2 : val=c1.deletion();
cout<<"Deleted Element :"<<val<<endl;
break;
case 3 : c1.display();
break;
}
cout<<"Do you want to continue<Y/N> ?";
cin>>op;
}while(op=='Y' || op=='y');
getch();
}