‹ Back to the paper
Write the program described below.
Specify the class CirQueue giving details of the functions void push(int) and int pop( ). Assume…
Specify the class CirQueue giving details of the functions void push(int) and int pop( ). Assume that the other functions have been defined.
The main function and algorithm need NOT be written.
Show the case
A Circular queue is a linear data structure which works on the principle of FIFO, enables the user to enter data from the rear end and remove data from the front end with the rear end connected to the front end to form a circular pattern. Define a class CirQueue with the following details:
Class name : CirQueue
Data members / instance variables:
cq[ ] : array to store the integers
cap : stores the maximum capacity of the array
front : to point the index of the front end
rear : to point the index of the rear end
Member functions:
CirQueue (int max) : constructor to initialize the data member cap=max, front=0 and rear=0
void push(int n) : to add integer in the queue from the rear end if possible, otherwise display the message “QUEUE IS FULL”
int pop( ) : removes and returns the integer from the front end of the queue if any, else returns -9999
void show( ) : displays the queue elements
Answer
Answer
AIvoid push(int n)
{
if ((rear + 1) % cap == front)
System.out.println("QUEUE IS FULL");
else
{
cq[rear] = n;
rear = (rear + 1) % cap;
}
}
int pop()
{
if (front == rear)
return -9999;
int v = cq[front];
front = (front + 1) % cap;
return v;
}Explanation: front and rear both start at 0, so front == rear means the queue is empty. push() checks whether advancing rear (with wrap-around using % cap) would meet front; if so the queue is full and the message QUEUE IS FULL is shown (one position is kept unused to tell a full queue from an empty one). Otherwise the number is stored at cq[rear] and rear moves to the next position circularly. pop() returns -9999 when the queue is empty; otherwise it returns the element at front and moves front forward circularly. Tested with cap = 4: three pushes were accepted, the fourth printed QUEUE IS FULL, pops returned the elements in FIFO order, and a pop on the empty queue returned -9999.From ISC 2020 Computer Science Paper 1, question 10(a).