Stack of Names ISC Computer Science 2011 Theory

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

The details of the class Stack is given below:

Class name: Stack

Data members/instance variables:
st[]: the array to hold the names
size: the maximum capacity of the string array
top: the index of the topmost element of the stack
ctr: to count the number of elements of the stack

Member functions:
Stack(): default constructor
Stack(int cap): constructor to initialize size = cap and top = -1
void pushName(String n): to push a name into the stack. If the stack is full, display the message "OVERFLOW"
String popName(): removes a name from the top of the stack and returns it. If the stack is empty, display the message "UNDERFLOW"
void display(): display the elements of the stack

(a) Specify the class Stack giving details of the constructors, void pushName(String n), String popName() and void display(). The main() function and algorithm need not be written.

import java.util.Scanner;
class Stack{
    String st[];
    int size;
    int top;
    int ctr;
    public Stack(){
        top = -1;
        ctr = 0;
    }
    public Stack(int cap){
        size = cap;
        st = new String[size];
        top = -1;
        ctr = 0;
    }
    public void pushName(String n){
        if(top + 1 == size)
            System.out.println("OVERFLOW");
        else
            st[++top] = n;
    }
    public String popName(){
        if(top == -1)
            return "UNDERFLOW";
        else
            return st[top--];
    }
    public void display(){
        if(top == -1)
            System.out.println("UNDERFLOW");
        else{
            for(int i = 0; i <= top; i++)
                System.out.print(st[i] + " ");
            System.out.println();
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Stack size: ");
        int s = Integer.parseInt(in.nextLine());
        Stack obj = new Stack(s);
        while(true){
            System.out.println("1. Push name");
            System.out.println("2. Pop name");
            System.out.println("3. Display");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(in.nextLine());
            switch(choice){
            case 1:
                System.out.print("Name to be pushed: ");
                String n = in.nextLine();
                obj.pushName(n);
                break;
            case 2:
                n = obj.popName();
                if(n.equals("UNDERFLOW"))
                    System.out.println(n);
                else
                    System.out.println(n + " POPPED");
                break;
            case 3:
                obj.display();
                break;
            default:
                System.out.println("Bye!");
                return;
            }
        }
    }
}

(b) Under what principle does the above entity work?

Stack works on LIFO (Last 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