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

Answer the following about the data structure described.

Specify the class Diary giving details of the functions void pushadd(String) and String popadd( )…

Computer Science20194 marksProgram
Specify the class Diary giving details of the functions void pushadd(String) and String popadd( ). Assume that the other functions have been defined. The main function and algorithm need NOT be written.
Show the case
A linear data structure enables the user to add address from rear end and remove address from front. Define a class Diary with the following details: Class name : Diary Data members / instance variables: Q[ ] : array to store the addresses size : stores the maximum capacity of the array start : to point the index of the front end end : to point the index of the rear end Member functions: Diary (int max) : constructor to initialize the data member size=max, start=0 and end=0 void pushadd(String n) : to add address in the diary from the rear end if possible, otherwise display the message “ NO SPACE” String popadd( ) : removes and returns the address from the front end of the diary if any, else returns “?????” void show( ) : displays all the addresses in the diary

Answer

Answer

AI
void pushadd(String n)
{
    if (end == size)
        System.out.println("NO SPACE");
    else
    {
        Q[end] = n;
        end++;
    }
}

String popadd()
{
    if (start == end)
        return "?????";
    else
    {
        String val = Q[start];
        start++;
        return val;
    }
}
Explanation: pushadd(String) checks whether end has reached size (diary full) before storing the address n at index end and incrementing end; otherwise it prints "NO SPACE". popadd() checks whether the diary is empty (start==end), returning "?????" in that case; otherwise it returns the address at index start and advances start, so addresses always leave from the front in the order they were added. Tested (run for real, capacity 3): after 3 successful pushadd calls a 4th correctly printed "NO SPACE"; popadd() correctly returned the addresses in FIFO order and finally returned "?????" once the diary was emptied.
Data structures

From ISC 2019 Computer Science Paper 1, question 11(a).