Recycle Dequeue ISC Computer Science 2025 Theory Specimen

Recycle is an entity which can hold at most 100 integers. The chain enables the user to add and remove integers from both the ends i.e. front and rear.

Define a class Recycle with the following details:

Class name: Recycle
Data members/instance variables:
ele[]: the array to hold the integer elements
cap: stores the maximum capacity of the array
front: to point the index of the front
rear: to point the index of the rear
Methods/Member functions:
Recycle(int max): constructor to initialize the data cap = max, front = rear = 0 and to create the integer array
void pushFront(int v): to add integers from the front index if possible else display the message “full from front”.
int popFront(): to remove and return elements from front. If array is empty then return -999.
void pushRear(int v): to add integers from the front index if possible else display the message “full from rear”.
int popRear(): to remove and return elements from rear. If the array is empty then return -999.

(i) Specify the class Recycle giving details of the functions void pushFront(int) and popRear(). Assume that the other functions have been defined.
The main() functions and algorithm need not be written.

import java.util.Scanner;
class Recycle{
    int ele[];
    int cap;
    int front;
    int rear;
    public Recycle(int max){
        if(max > 100)
            max = 100;
        cap = max;
        ele = new int[cap];
        front = 0;
        rear = 0;
    }
    public void pushFront(int v){
        if(front == 0)
            System.out.println("full from front");
        else
            ele[--front] = v;
    }
    public int popFront(){
        if(front == rear)
            return -999;
        else{
            int v = ele[front++];
            if(front >= rear){
                front = 0;
                rear = 0;
            }
            return v;
        }
    }
    public void pushRear(int v){
        if(rear == cap)
            System.out.println("full from rear");
        else
            ele[rear++] = v;
    }
    public int popRear(){
        if(front == rear)
            return -999;
        else{
            int v = ele[--rear];
            if(front >= rear){
                front = 0;
                rear = 0;
            }
            return v;
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Dequeue capacity: ");
        int c = Integer.parseInt(in.nextLine());
        Recycle obj = new Recycle(c);
        while(true){
            System.out.println("1. Push front");
            System.out.println("2. Pop front");
            System.out.println("3. Push rear");
            System.out.println("4. Pop rear");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(in.nextLine());
            switch(choice){
            case 1:
                System.out.print("Element to be pushed: ");
                int v = Integer.parseInt(in.nextLine());
                obj.pushFront(v);
                break;
            case 2:
                v = obj.popFront();
                if(v == -999)
                    System.out.println("empty from front");
                else
                    System.out.println(v + " popped from front");
                break;
            case 3:
                System.out.print("Element to be pushed: ");
                v = Integer.parseInt(in.nextLine());
                obj.pushRear(v);
                break;
            case 4:
                v = obj.popRear();
                if(v == -999)
                    System.out.println("empty from rear");
                else
                    System.out.println(v + " popped from rear");
                break;
            default:
                System.out.println("Bye");
                return;
            }
        }
    }
}

(ii) Name the entity described above and state its principle.
Dequeue is the entity described above. It works on FIFO (First In First Out) principle.

Leave a Reply

Your email address will not be published. Required fields are marked *