Stack Java Program | ISC Computer Science 2022 Theory

A 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 end only.

The details of the class Stack are given below:

Class name: Stack
Data members/instance variables:
cha[]: array to hold the characters
size: stores the maximum capacity of the stack
top: to point the index of the topmost element of the stack
Member functions/methods:
Stack(int mm): constructor to initialize the data member size = mm, top = -1 and create the character array
void push_char(char v): to add characters from the top end if possible else display the message “Stack full”
char pop_char(): to remove and return characters from the top end, if any, else returns ‘$’
void display(): to display elements of the stack

Specify the class Stack, giving the details of void push_char(char) and char pop_char(). Assume that the other functions have been defined.

The main() function and algorithm need not be written.

import java.util.Scanner;
class Stack{
    char cha[];
    int size;
    int top;
    public Stack(int mm){
        size = mm;
        top = -1;
        cha = new char[size];
    }
    public void push_char(char v){
        if(top + 1 == size)
            System.out.println("Stack full");
        else
            cha[++top] = v;
    }
    public char pop_char(){
        if(top == -1)
            return '$';
        else
            return cha[top--];
    }
    public void display(){
        if(top == -1)
            System.out.println("Stack empty");
        else{
            for(int i = 0; i <= top; i++)
                System.out.print(cha[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 character");
            System.out.println("2. Pop character");
            System.out.println("3. Display characters");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(in.nextLine());
            switch(choice){
            case 1:
                System.out.print("Character to be pushed: ");
                char ch = in.nextLine().charAt(0);
                obj.push_char(ch);
                break;
            case 2:
                ch = obj.pop_char();
                if(ch == '$')
                    System.out.println("Stack empty");
                else
                    System.out.println(ch + " popped");
                break;
            case 3:
                obj.display();
                break;
            default:
                System.out.println("Bye");
                return;
            }
        }
    }
}

Leave a Reply

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