Stack using array



Program In Java To Implement Stack Using Array

The Following Program Reads The Stack Size From User And Displays Menu Containing Stack Operations And Reads Users Choice And Works According To Users Input And Displays Message Accordingly. This Menu Is In Infinite Loop Until User Enters Exit.

import java.io.*;
class MyStack {
    int[] a;
    int top;
    /*
        Constructors
    */
    MyStack() //initialize the stack in a default manner
    {
        a = new int[5];
        top = -1;
    }
    MyStack(int size) //initialize the stack in a said manner
    {
        a = new int[size];
        top = -1;
    }
    /*
        Functions
    */
    void display() //displaying the original stack
    {
        if (!empty()) {
            System.out.println("\n\nEntered Stack Values Are:: ");
            for (int j = top; j >= 0; j--) System.out.println("\t\t\t   " + a[j]);
        } else System.out.println("\n\tStack Is Empty.");
    }
    /*
        Stack Functions
    */
    boolean empty() {
        return (top == -1) ? true : false;
    }
    void push(int ele) {
        if (top < a.length - 1) {
            a[++top] = ele;
            System.out.println("\nYour Data Has Successfully Been Entered.");
        } else System.out.println("\n\tStack Overflow.");
    }
    String pop() {
        if (!empty()) return "\n\tPopped Element : " + a[top--];
        else return "\n\tStack is Empty.";
    }
    /*
        Main Function
    */
    public static void main(String[] arg) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\nEnter Your Stack Size:: ");
        int s = Integer.parseInt(br.readLine());
        int m;
        MyStack st = new MyStack(s);
        while (true) //displaying the menu
        {
            System.out.println("\n\n\t\t\tImplementation Of Stack");
            System.out.println("\n\tSelect From Below What You Want To Do : ");
            System.out.println("\n 1. Push.\n 2. Pop.\n 3. Display.\n 4. Exit.");
            System.out.print("\nEnter Your Choice : ");
            m = Integer.parseInt(br.readLine());
            switch (m)
            /*transferring the input
                  to appropriate position*/
            {
                case 1: //Inserting Element In Stack
                    System.out.print("\n\tEnter Integer Element : ");
                    int e = Integer.parseInt(br.readLine());
                    st.push(e);
                    break;
                case 2: //Removing Element From Stack
                    System.out.println(st.pop());
                    break;
                case 3: //Displaying The Stack
                    st.display();
                    break;
                case 4: //Exiting
                    System.out.println("\n\n\tExited Successfully\n");
                    System.exit(0);
                    break;
                default:
                    System.out.println("\n\tInvalid Entry.");
                    System.out.println("\tEnter Proper Input.");
                    break;
            }
        }
    }
}


OUTPUT::


Enter Your Stack Size::5

Implementation Of Stack

Select From Below What You Want To Do:
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 1

Enter Integer Element: 12

Your Data Has Successfully Been Entered.

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 1

Enter Integer Element: 23

Your Data Has Successfully Been Entered.

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 1

Enter Integer Element: 34

Your Data Has Successfully Been Entered.

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 3

Entered Stack Values Are::34
23
12

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 1

Enter Integer Element: 45

Your Data Has Successfully Been Entered.

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 1

Enter Integer Element: 56

Your Data Has Successfully Been Entered.

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 3

Entered Stack Values Are::56
45
34
23
12

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 1

Enter Integer Element: 67

Stack Overflow.

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 2

Popped Element: 56

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 2

Popped Element: 45

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 3

Entered Stack Values Are::34
23
12

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 2

Popped Element: 34

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 3

Entered Stack Values Are::23
12

Implementation Of Stack

Select From Below What You Want To Do: 
1. Push.
2. Pop.
3. Display.
4. Exit.

Enter Your Choice: 4

Exited Successfully
Previous Post Next Post