Holder Queue ISC Specimen 2023 Theory

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.

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) Specify the class Holder giving details of the function void addInt(int) and int removeInt(). Assume that the other functions have been defined.

import java.util.Scanner;
class Holder{
    int q[];
    int cap;
    int front;
    int rear;
    public Holder(int n){
        cap = n;
        q = new int[cap];
        front = 0;
        rear = 0;
    }
    public void addInt(int v){
        if(rear == cap)
            System.out.println("HOLDER IS FULL");
        else
            q[rear++] = v;
    }
    public int removeInt(){
        int v = 0;
        if(front == 0 && rear == 0)
            return -999;
        else{
            v = q[front++];
            if(front >= rear){
                front = 0;
                rear = 0;
            }
        }
        return v;
    }
    public void show(){
        if(front == 0 && rear == 0)
            System.out.println("HOLDER IS EMPTY");
        else{
            for(int i = front; i < rear; i++)
                System.out.print(q[i] + " ");
            System.out.println();
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Holder capacity: ");
        int c = Integer.parseInt(in.nextLine());
        Holder obj = new Holder(c);
        while(true){
            System.out.println("1. Add Integer");
            System.out.println("2. Remove Integer");
            System.out.println("3. Display Integers");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(in.nextLine());
            switch(choice){
            case 1:
                System.out.print("Enter the integer: ");
                int n = Integer.parseInt(in.nextLine());
                obj.addInt(n);
                break;
            case 2:
                n = obj.removeInt();
                if(n == -999)
                    System.out.println("HOLDER IS EMPTY");
                else
                    System.out.println(n + " REMOVED");
                break;
            case 3:
                obj.show();
                break;
            default:
                System.out.println("Bye!");
                return;
            }
        }
    }
}

(ii) Name the entity described above and state its principle.

The above entity described is a queue. It is based on the FIFO (First In First Out) Principle.

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory