‹ Back to the paper
Answer the questions on the class described below.
Holder is a kind of data structure which can store elements with the restriction that an element…
Holder is a kind of data structure which can store elements with the restriction that an element can be added from the rear end and removed from the front end only.
The details of the class Holder is given below:
| Class name | : | Holder |
|---|---|---|
| Data members/instance variables: | ||
| Q[ ] | : | array to hold integers |
| cap | : | maximum capacity of the holder |
| front | : | to point the index of the front end |
| rear | : | to point the index of the rear end |
| Methods / Member functions: | ||
| Holder(int n ) | : | constructor to initialize cap=n, front= 0 and rear=0 |
| void addint( int v ) | : | to add integers in the holder at the rear end if possible, otherwise display the message “ HOLDER IS FULL ” |
| int removeint( ) | : | removes and returns the integers from the front end of the holder if any, else returns −999 |
| void show( ) | : | displays the elements of the holder |
(i)[4.0]
Specify the class Holder giving details of the functions void addint(int) and int removeint( ). Assume that the other functions have been defined.
The main( ) function and algorithm need NOT be written.
(ii)[1.0]
Name the entity described above and state its principle.
Answer
Answer (i)
AIvoid addint(int v)
{
if (rear == cap)
System.out.println("HOLDER IS FULL");
else
{
Q[rear] = v;
rear++;
}
}
int removeint()
{
if (front == rear)
return -999;
else
{
int val = Q[front];
front++;
return val;
}
}Explanation: addint() checks whether rear has reached the capacity cap (holder full) before storing v at index rear and advancing rear; otherwise it prints "HOLDER IS FULL". removeint() checks whether the holder is empty (front==rear), returning -999 in that case; otherwise it returns the element at front and advances front, so elements always leave from the front in the order they were added (verified logically against the given constructor, which sets front=rear=0).Answer (ii)
AIThe entity described is a Queue (Linear Queue). It works on the principle of FIFO (First In First Out) - the element added first (at the rear) is the element removed first (from the front).
From ISC 2023 Specimen Computer Science Paper 1, question 9.