Java program to implement Stack – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Tue, 16 Aug 2022 16:18:23 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Java program to implement Stack – Programmerbay https://programmerbay.com 32 32 Java Program to Implement Stack https://programmerbay.com/java-program-to-implement-stack/ https://programmerbay.com/java-program-to-implement-stack/#respond Tue, 14 Jun 2022 16:59:58 +0000 https://programmerbay.com/?p=5100 The program shows stack implementation. In this article, we’ll be writing a custom stack implementation with the following operations pop, peek, and push. Apart from that, we’ll be using predefined class to achieve the stack functionality.

A stack is a data structure that works in LIFO ( Last in First out) manner. Data can be inserted with push operation and deleted using pop operation, only from one end called Top.

Java program to implement Stack using Custom Implementation

Program:

import java.util.Scanner;
class StackExample {
    int number[];
    int top;
    int limit;

    // constructor
    public StackExample(int size){
        top =-1;
        limit =size;
        number=new int[size];
    }
    // insert an element
    void push(int num)
    {
        if(isFull())
            System.out.println("Stack is full");
        else
        {
            top=top+1;
            number[top]=num;
        }

    }
    // popping out the element that is at the top of stack
    void pop(){
        if(isEmpty())
            System.out.println("Stack is Empty");
        else{
            top=top-1;
            System.out.println("Element popped out");
        }
    }
    void peek()
    {
        System.out.println("Top most element in Stack ="+number[top]);

    }
    // check stack, is it full or not?
    boolean isFull(){
        return top >=limit-1;
    }
    boolean isEmpty(){
        return top==-1;
    }

    void print(){
        System.out.println("Elements in Stack:-");
        for(int i=top;i>=0;i--)
            System.out.println(number[i]);
    }


    public static void main(String[] args) {
        Scanner input =new Scanner(System.in) ;
        int size,option,element;
        char chr;
        System.out.print("Enter the maximum size that a stack can have = ");
        size=input.nextInt();
        StackExample obj= new StackExample(size);
        do{
            System.out.println("Please press any number from the following operations:-"
                    + "\n 1. Insert an element "
                    + "\n 2. Pop an element"
                    + "\n 3. Peek of the stack"
                    + "\n 4. Display the elements of the stack");
            option=input.nextInt();

            switch(option){
                case 1:
                    System.out.print("Please enter the element to insert = ");
                    element=input.nextInt();
                    obj.push(element);
                    break;
                case 2:
                    obj.pop();
                    break;
                case 3:
                    obj.peek();
                    break;
                case 4:
                    obj.print();
                    break;
                default:
                    System.out.println("Choose wrong option ");

            }
            System.out.println("Want to continue? y or n ");
            chr=input.next().charAt(0);
        }while(chr=='y'||chr=='Y');
    }
}

Output:

Java program to implement Stack using Stack Class

Java supports a predefined class named Stack. It offers methods like push(), pop() and peek() that helps in achieving stack implementation

Program: 

import java.util.Stack;

class StackExample {
    public static void main(String[] args) {
        Stack<Integer> integerStack = new Stack<>();
        // Pushing 5 element into the stack
        integerStack.push(34);
        integerStack.push(2);
        integerStack.push(5);
        integerStack.push(100);
        integerStack.push(2);

        // printing the stack's elements
        System.out.println("Elements in the Stack :: " + integerStack);

        // Pop operation on the stack, 2 would be removed from the top
        integerStack.pop();

        // printing the stack's elements
        System.out.println("Elements in the Stack :: " + integerStack);

        // Peek operation on the stack,100 would be printed
        System.out.println("Top of the stack :: " + integerStack.peek());
    }
}

Output:

Elements in the Stack :: [34, 2, 5, 100, 2]
Elements in the Stack :: [34, 2, 5, 100]
Top of the stack :: 100

]]>
https://programmerbay.com/java-program-to-implement-stack/feed/ 0