‹ Back to the paper
Question 9
5 marks
Computer ScienceISC 2025 Improvement Computer Science Paper 1Case basedData structures
Answer the following on the class described.
A circular queue is a linear data structure that allows data insertion at the rear and removal from…
A circular queue is a linear data structure that allows data insertion at the rear and removal from the front, with the rear end connected to the front end forming a circular arrangement.
Given below are the details of class MerryGoRound
Class name : MerryGoRound
Data members/instance variables:
q[ ] : an array to hold integers
cap : to store the maximum capacity of the array
front : to point the index of the front end
rear : to point the index of the rear end
Methods/Member functions:
MerryGoRound(int n) : parameterised constructor to initialise cap = n, front = rear = 0
void add(int val) : to add integers from the rear index if possible, else display the message "QUEUE IS FULL"
int remove( ) : to remove and return the integer from front if any, else return -999
void display( ) : to display the elements of the circular queue in the order of front to rear
(i)[4]
Specify the class MerryGoRound giving details of the functions void add(int) and int remove( ). Assume that the other functions have been defined.
The main( ) function and algorithm need NOT be written.
(ii)[1]
State the principle on which the entity works.
Answer
Answer (i)
void add(int val)
{
if ((rear + 1) % cap == front)
System.out.println("QUEUE IS FULL");
else
{
q[rear] = val;
rear = (rear + 1) % cap;
}
}
int remove()
{
if (front == rear)
return -999;
int val = q[front];
front = (front + 1) % cap;
return val;
}Explanation: add() checks whether inserting at rear would collide with front (queue full) using modulo arithmetic to wrap around the array; if not full it stores val at q[rear] and advances rear circularly. remove() checks for an empty queue (front==rear) returning -999 in that case; otherwise it returns the element at front and advances front circularly. Tested with cap=4: after add(10),add(20),add(30), remove() correctly returns 10; then add(40) succeeds but a further add(50) correctly prints 'QUEUE IS FULL', and the queue then displays as 20 30 40.Answer (ii)
A circular queue works on the principle of FIFO (First In First Out), with the added feature that the rear end is logically connected back to the front end, forming a circular arrangement so that vacated front spaces can be reused instead of the queue getting prematurely full.