Implementation of Stack in Java

A stack is an abstract data type that serves as a collection of elements, with two principal operations:

  1. push adds an element to the collection
  2. pop removes the last element that was added.

It is a LIFO (Last In First Out) kind of data structure. That means the element which added at last will be taken out first.

import java.io.*;
class stack {
    int size;
    int item[];
    int top;

    public stack() {
        size = 10;
        item = new int[size];
        top = -1;
    }
    public void push(int ele) {
        if (top == (size - 1)) {
            System.out.println("Stack Overflow");
        } else {
            top++;
            item[top] = ele;
        }
    }
    public int pop() {
        if (top == -1) {
            System.out.println("No Elements");
            return (-1);
        } else {
            int x = item[top];
            top--;
            return (x);
        }
    }
    public int peek() {
        if (top == -1) {
            System.out.println("No Elements");
            return (-1);
        } else
            return (item[top]);
    }
    public void display() {
        System.out.println();
        if (top == -1) {
            System.out.println("No Elements");
        } else {
            System.out.println("Stack is");
            for (int i = 0; i <= top; i++)
                System.out.println(item[i]);
        }
    }
}

class stackank {
    public static void main(String args[]) throws IOException {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        stack s = new stack();
        int e, ch;
        do {
            System.out.println();
            System.out.println("Stack Implementation");
            System.out.println("1.Add Element to Stack");
            System.out.println("2.Remove Element From Stack");
            System.out.println("3.Peek");
            System.out.println("4.Display Stack");
            System.out.println("5.Exit");
            System.out.println("Select Option");
            ch = Integer.parseInt(obj.readLine());
            switch (ch) {
                case 1:
                    {
                        System.out.println("Enter Number to Push");
                        e = Integer.parseInt(obj.readLine());
                        s.push(e);
                        break;
                    }
                case 2:
                    {
                        System.out.println("Poped Element is" + s.pop());
                        break;
                    }
                case 3:
                    {
                        System.out.println("On Implementing peek the result is " + s.peek());
                        break;
                    }
                case 4:
                    {
                        s.display();
                        break;
                    }
                case 5:
                    {
                        break;
                    }
            }
        } while (ch != 5);
    }
}


/*

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
1

Enter Number to Push
10

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
4

Stack is
10

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
1

Enter Number to Push
20

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
4

Stack is
10
20

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
1

Enter Number to Push
30

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
4

Stack is
10
20
30

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
3
O
n Implementing peek the result is 30

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
2

Poped Element is30

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
4

Stack is
10
20

Stack Implementation
1.Add Element to Stack
2.Remove Element From Stack
3.Peek
4.Display Stack
5.Exit
Select Option
5
Process Exit...

*/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.