Stack Data Structure in Java

Data Structures are a key concept in computer programming. Programmers should be through with the understanding and implementation of data structures. There are many data structures like stack, queue, lists, trees and so on.
Today, in this tutorial, we are going to learn about the Stack data structure in Java and it’s implementation.

First of all, What is the stack?
Stack is a data structure that implements First in First out (FIFO) rule.
In stack we can delete or add an element from one end, and this end is called top.
Functions of a stack:

  • push(): This method is used for inserting an element
  • pop(): This method is used for removing an element
  • peek(): The peek() method tells us the value of the element at the top
  • isFull(): This is a method that returns true if the stack is full else it returns false
  • isEmpty(): This method returns true if the stack is empty else it returns false

Now, let’s check its implementation.

Stack class in Java

As the functions of stack are known, keeping them in mind, let us define a stack class:

class Stack{
  int[] data;
  int capacity; 
  int top;   
}
  • data array stores the elements
  • capacity defines maximum amount of data, a stack can store
  • top keeps track of the top of the stack

A parameterized  constructor is required to initialize top and capacity:

class Stack{
  int[] data;
  int capacity; 
  int top;   
  
  Stack(int capacity){//constructor
        top = -1;
        data = new int[capacity];
    }
}

Since, initially, stack is empty, it is initialized to -1.

push()

public void push(int i){
    if(top<capacity){
        top++;
        data[top]=i;
    }
    else
        System.out.println("Full Stack");
}

push() accepts an element as parameter and inserts it on top of the stack only if stack is not full which is checked by the condition if(top<capacity) else  it prints “Full stack”.

pop()

public int pop(){
    if(top<0)
        return -1;
    int ele=data[top];
    top--;
    return ele;
}

pop() returns the top most element and decreases the value of top.
If the stack is empty then it’s checked using the condition if(top<0) and -1 is returned indicating that stack is empty else an element is returned.

peek()

public int peek(){
    if(top<=0)
        return;
    return data[top];
}

peek() returns nothing if the stack is empty which is checked by the condition if(top<=0) . If it’s not empty then it returns the top element of the stack.

isFull()

public boolean isFull(){
    if(top>capacity)
        return true;
    return false;            
}

top is used to determine if the stack is full. If top is greater than capacity, it means that stack is full and true is returned else false is returned.

isEmpty()

public boolean isEmpty(){
    if(top <= -1)
        return true;
    return false;            
}

Also, read:

The top is used to determine if the stack is empty. If top is less than or equal to -1, it means that stack is empty and true is returned else false is returned.

Leave a Reply

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