Back Source

A register is an entity which can hold a maximum of 100 names. The register enables the user to add and remove names from the topmost end only (a) Specify the class Register giving details of the functions void push(String) and String pop(). Assume that the other functions have been defined. The main function and algorithm need NOT be written. (b) Name the entity used in the above data structure arrangement.

class Stack {
    protected String[] stud;
    protected int size, top = -1;

    Stack(int max) {
        size = max;
        stud = new String[size];
    }

    void push(String n) {
        if (top < size - 1) {
            stud[++top] = n;
        } else {
            System.out.println("StatckOverflow");
        }
    }

    String pop() {
        if (top != -1) {
            return stud[top--];
        } else {
            System.out.println("StatckUnderFlow");
            return "$$";
        }
    }

    void display() {
        for (int x = 0; x < top; x++)
            System.out.println(stud[x]);
    }
}

public class Register {
    public static void main(String[] args) {

    }
}