‹ Back to the paper
Question 8
4 marks
Computer ScienceISC 2022 Computer Science - Specimen paper (Semester 2), Paper 1ProgramData structures
Write the program described below.
A Queue is a linear data structure in which the operations are performed based on FIFO (First In…
A Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out).
Define a class Queue with the following details:
Class name: Queue
Data member/instance variable:
dat[]: array to hold the integer elements
cap: stores the maximum capacity of the queue
front: to point the index of the front
rear: to point the index of the rear
Member functions/methods:
Queue(int max): constructor to initialize the data member cap = max, front = rear = 0 and create the integer array
void add_dat(int v): to add integers from the rear index if possible else display the message (“Queue full”)
int pop_dat(): to remove and return elements from front, if any, else returns -999
void display(): to display elements of the queue
Specify the class Queue giving the details of void add_dat(int) and int pop_dat(). Assume that the other functions have been defined.
The main() function and algorithm need NOT be written.
Answer
Answer
void add_dat(int v)
{
if (rear < cap)
{
dat[rear] = v;
rear++;
}
else
System.out.println("Queue full");
}
int pop_dat()
{
if (front == rear)
return -999;
int v = dat[front];
front++;
return v;
}Elements are added at index rear while rear is less than cap; otherwise "Queue full" is displayed. Elements are removed from index front, and -999 is returned when the queue is empty (front equals rear).