PRASHNIKAप्रश्निका
Back to the paper

Shelf is a kind of data structure which can store elements with the restriction that an element can…

Computer Science20245 marksProgram
Shelf 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 Shelf are given below: Class name : Shelf Data members/instance variables: ele[ ] : array to hold decimal numbers lim : maximum limit of the shelf front : to point the index of the front end rear : to point the index of the rear end Methods / Member functions: Shelf(int n ) : constructor to initialize lim=n, front= 0 and rear=0 void pushVal(double v) : to push decimal numbers in the shelf at the rear end if possible, otherwise display the message “ SHELF IS FULL ” double popVal( ) : to remove and return the decimal number from the front end of the shelf if any, else returns −999.99 void display( ) : to display the elements of the shelf
(i)[4.0]
Specify the class Shelf giving details of the functions void pushVal(double) and double popVal( ). 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)

AI
void pushVal(double v)
{
    if (rear == lim)
        System.out.println("SHELF IS FULL");
    else
    {
        ele[rear] = v;
        rear++;
    }
}

double popVal()
{
    if (front == rear)
        return -999.99;
    else
    {
        double v = ele[front];
        front++;
        return v;
    }
}
Explanation: pushVal() checks whether rear has reached the limit lim (shelf full) before inserting v at the rear index and advancing rear. popVal() checks whether the shelf is empty (front==rear), returning -999.99 in that case; otherwise it returns the element at front and advances front. Tested (run for real) with lim=3: pushing 1.5, 2.5, 3.5 succeeded, a fourth pushVal correctly printed "SHELF IS FULL", and popVal() correctly returned 1.5 then 2.5 in that order, leaving 3.5 in the shelf.

Answer (ii)

AI
The entity described is a Queue (Linear Queue). It works on the principle of FIFO (First In First Out) - the element inserted first (at the rear) is the element removed first (from the front).
Data structures

From ISC 2024 Specimen Computer Science Paper 1, question 9.