Rack Stack Java Program | ISC Computer Science 2021

Rack is a kind of data structure which can store elements with the restriction that an element can be added or removed from top end only.

The details of the class Rack are given below:

Class name: Rack
Data members/instance variables:
s[]: array to hold integers
size: maximum capacity of the rack
top: to point the index of the top end
Methods/Member functions:
Rack(int cap): constructor to initialize size = cap and top = -1
void push(int v): to add integers in the rack at the top location if possible, otherwise display the message “RACK IS FULL”
int pop(): removes and returns the integers from the topmost location of the rack if any, else returns -9999
void display(): displays the elements of the rack

(a) Specify the class Rack giving details of the functions void push(int) and int pop(). Assume that the other functions have been defined. The main() function and algorithm need not be written.

import java.util.Scanner;
class Rack{
    int s[];
    int size;
    int top;
    public Rack(int cap){
        size = cap;
        top = -1;
        s = new int[size];
    }
    public void push(int v){
        if(top + 1 == size)
            System.out.println("RACK IS FULL");
        else
            s[++top] = v;
    }
    public int pop(){
        if(top == -1)
            return -9999;
        return s[top--];
    }
    public void display(){
        if(top == -1)
            System.out.println("RACK IS EMPTY");
        else{
            for(int i = 0; i <= top; i++)
                System.out.print(s[i] + " ");
            System.out.println("<-- top");
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Rack size: ");
        int cap = Integer.parseInt(in.nextLine());
        Rack obj = new Rack(cap);
        while(true){
            System.out.println("1. Push");
            System.out.println("2. Pop");
            System.out.println("3. Display");
            System.out.println("4. Exit");
            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.push(v);
                break;
            case 2:
                v = obj.pop();
                if(v == -9999)
                    System.out.println("RACK IS EMPTY");
                else
                    System.out.println(v + " POPPED");
                break;
            case 3:
                obj.display();
                break;
            default:
                System.out.println("Bye!");
                return;
            }
        }
    }
}

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

The entity is a stack data structure, which is based on LIFO (Last In First Out) principle.

Leave a Reply

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