Answer the questions on the class described 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 |
Show 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).