Introduction to Stack in Java

Introduction to stack

Stack in Java

A Stack is a linear data structure that follows the principle of (Last-In-First-Out) LIFO . In Stack  there is one end through which insertion and deletion takes place.  Whenever an element is added in the stack, it is added on the top of the stack, and the element can be deleted only from the stack. In other words, a stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack.

Operation in stack:

Following are common operations implemented on the stack:

  • push():When we insert an element in a stack then the operation is known as a push. If the stack is full then the condition is called overflow condition.
  • pop(): When we want to delete an element from the stack, the operation is known as a pop. If the stack is empty means there is no element in the stack, this condition is known as an underflow state.
  • isEmpty():When we want to determines whether the stack is empty or not.
  • isFull(): when we want to determines whether the stack is full or not.’
  • peek(): It returns the element at the given position.

                                                                                           Implementation of stack :

Algorithm:

  • Push operation : 

    Push operation consists  following two steps.

    • Increment Top so that it can now refer to the next memory location.
    • Add element at position of incremented top variable . This position is where new element is added.
  • Pop Operation: 

pop operation is used to delete element from top if the stack see code below  

int pop()
{
if (top < 0) {
System.out.println(“Underflow condition reached”);
return 0;
}
else {
int x = a[top–];
return x;
}
}
  • peek operation:

Peek operation used to return the element which is present at the top of the stack but it does not delete it . Underflow condition occur when we try to return the top element in empty stack.

int peek()
{
if (top < 0) {
System.out.println(“Underflow condition”);
return 0;
}
else {
int x = a[top];
return x;
}
}

Java code to implement stack using array:

/* Java program to implement stack
operations using array*/
class Stack {
    static  int MAX = 100;
    int top;
    int a[] = new int[MAX]; // Maximum size of Stack

 

    boolean isEmpty()
    {
        return (top < 0);
    }
    Stack()
    {
        top = –1;
    }

 

    boolean push(int x)
    {
        if (top >= (MAX – 1)) {
            System.out.println(“Overflow condition reached”);
            return false;
        }
        else {
            a[++top] = x;
            System.out.println(x + ” pushed into stack”);
            return true;
        }
    }

 

    int pop()
    {
        if (top < 0) {
            System.out.println(“Underflow condition reached”);
            return 0;
        }
        else {
            int x = a[top–];
            return x;
        }
    }

 

    int peek()
    {
        if (top < 0) {
            System.out.println(“Underflow condition”);
            return 0;
        }
        else {
            int x = a[top];
            return x;
        }
    }
}
class Prepinsta {
    public static void main(String args[])
    {
        Stack stk = new Stack();
        stk.push(20);
        stk.push(40);
        stk.push(60);
        System.out.println(stk.pop());
    }
}

Java code to implement stack using linked list :


// Java Code for Linked List Implementation

public class LinkedList {

    stacknode node;

    static class stacknode {
        int data;
        stacknode next;

        stacknode(int data) { this.data = data; }
    }

    public boolean isEmpty()
    {
        if (node == null) {
            return true;
        }
        else
            return false;
    }

    public void push(int data)
    {
        stacknode newNode = new stacknode(data);

        if (node == null) {
            node = newNode;
        }
        else {
            stacknode temp = node;
            node = newNode;
            newNode.next = temp;
        }
        System.out.println(data);
    }

    public int pop()
    {
        int popped = Integer.MIN_VALUE;
        if (node == null) {
            System.out.println(“Stack is Empty”);
        }
        else {
            popped = node.data;
            node = node.next;
        }
        return popped;
    }

    public int peek()
    {
        if (node == null) {
            System.out.println(“Stack is empty”);
            return Integer.MIN_VALUE;
        }
        else {
            return node.data;
        }
    }

    
    public static void main(String[] args)
    {

        LinkedList sll = new LinkedList();

        slk.push(20);
        slk.push(40);
        slk.push(60);

        System.out.println(slk.pop());

        System.out.println(“Top element is “ + slk.peek());
    }
}
Output :

60