DataStructure

What is Stack Data Structure in Java? Complete Beginner’s Guide

Spread the love

Stack is liner data structure , in stack you can insert and remove the data from top. stack is last in first out(LIFO). We can push and pop data from top .

Stack is a linear data structure that follows the last in first out (LIFO) principle . this means that the last element added to the stack is the first one to be removed . think of it like a stack of plates you add plates to the top and also removed them from the top .

Basic operations
  • Push : Add an element to the top of the stack .
  • Pop : Removed the top element from the stack .
  • Peek/top : view the top element without removing .
  • isEmpty: check if the stack is empty .

Where to use stacks

  • Function Call Management
    • The call stack in programming languages keeps track of function call and returns .
  • Undo Mechanism
    • many application, like text editor , use stack to implement undo functionality .
  • Backtracking Algorithm
    • Such as solving mazes , puzzels and navigating tree structures .

1.Stack Implementation using array

Stack Empty Design

package com.test.rkdigital.school.stack;

public class Stack {

	private int[] stackArray;
	private int top;
	private int maxSize; 
	
	// Constructor to initialize the stack  
	public Stack(int size) {
		// TODO Auto-generated constructor stub
		maxSize=size;
		stackArray=new int[maxSize];
		top=-1;
	}
	
	// Method to add an element to the stack 
	public void push(int data) {
		if(isFull()) {
			System.out.println("Stack is full, cannot push data"+data);
			return;
		}
		stackArray[++top]=data;
	}
	
	//Method to remove an element from the stack  
	public int pop() {
		if(isEmpty()) {
			System.out.println("Stack is Empty");
			return -1;
		}
		return stackArray[top--];
	}

	//method to view the top element of the stack
	public int peek()
	{
		if(isEmpty()) {
			System.out.println("stack is empty ,can not be peek");
			return -1;
		}
		return stackArray[top];
	}
	
	public boolean isEmpty() {
		return top==-1;
	}
	public boolean isFull() {
		return top==maxSize-1;
	}
	
	
	public static void main(String[] args) {
		Stack stack=new Stack(5);
		stack.push(10);
		stack.push(20);
		stack.push(30);
		
		System.out.println("top element is: "+stack.peek());
		
		System.out.println("pop element is: "+stack.pop());
		
	
		System.out.println("top element is: "+stack.peek());
		
	}
}
2.Write a program to Reverse string using stack
import java.util.Stack;

public class ReverseString {
    public static void main(String[] args) {
        String str = "RKDIGITALSCHOOL";
        Stack<Character> stack = new Stack<>();

        for(char ch : str.toCharArray()) {
            stack.push(ch);
        }

        String reversed = "";
        while(!stack.isEmpty()) {
            reversed += stack.pop();
        }

        System.out.println("Reversed String: " + reversed);
    }
}

Leave a Reply

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