How to create stack in Java?

Creating a Stack in Java: A Comprehensive Guide

What is a Stack?

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It means that the last element added to the stack will be the first one to be removed. Stacks are often used in programming to implement functions such as parsing, evaluating postfix expressions, and implementing recursive algorithms.

Why Use a Stack in Java?

Stacks are useful in various scenarios, including:

  • Evaluating postfix expressions
  • Implementing recursive algorithms
  • Parsing syntax in programming languages
  • Managing function calls and returns

Creating a Stack in Java

Here’s a step-by-step guide to creating a stack in Java:

Step 1: Define the Stack Class

Create a new Java class called Stack to represent the stack data structure. This class will have the following methods:

  • push(E element): adds an element to the top of the stack
  • pop(): removes the top element from the stack
  • peek(): returns the top element without removing it
  • isEmpty(): checks if the stack is empty
  • size(): returns the number of elements in the stack

public class Stack {
private Node top;
private int size;

public Stack() {
top = null;
size = 0;
}

// Push an element onto the stack
public void push(E element) {
Node newNode = new Node(element);
if (top == null) {
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
size++;
}

// Pop an element from the stack
public E pop() {
if (isEmpty()) {
throw new RuntimeException("Stack is empty");
}
E element = top.data;
top = top.next;
size--;
return element;
}

// Peek at the top element without removing it
public E peek() {
if (isEmpty()) {
throw new RuntimeException("Stack is empty");
}
return top.data;
}

// Check if the stack is empty
public boolean isEmpty() {
return top == null;
}

// Get the number of elements in the stack
public int size() {
return size;
}

// Node class to represent a single element in the stack
private static class Node {
E data;
Node next;

public Node(E data) {
this.data = data;
this.next = null;
}
}
}

Step 2: Create a Main Class to Test the Stack

Create a new Java class called Main to test the stack:

public class Main {
public static void main(String[] args) {
Stack stack = new Stack();

// Push elements onto the stack
stack.push(10);
stack.push(20);
stack.push(30);

// Peek at the top element
System.out.println("Top element: " + stack.peek());

// Pop elements from the stack
System.out.println("Popped element: " + stack.pop());
System.out.println("Popped element: " + stack.pop());

// Check if the stack is empty
System.out.println("Is stack empty? " + stack.isEmpty());

// Get the size of the stack
System.out.println("Stack size: " + stack.size());
}
}

Stack Operations

Here’s a table summarizing the stack operations:

Operation Description
push(E element) Adds an element to the top of the stack
pop() Removes the top element from the stack
peek() Returns the top element without removing it
isEmpty() Checks if the stack is empty
size() Returns the number of elements in the stack

Example Use Cases

Here are some example use cases for the stack:

  • Evaluating postfix expressions: A stack can be used to evaluate postfix expressions by pushing operators onto the stack and popping them off when they encounter a number.
  • Implementing recursive algorithms: A stack can be used to implement recursive algorithms by pushing function calls onto the stack and popping them off when the function returns.
  • Parsing syntax in programming languages: A stack can be used to parse syntax in programming languages by pushing tokens onto the stack and popping them off when the parser encounters a token.

Conclusion

In this article, we’ve covered the basics of creating a stack in Java. We’ve defined the stack class, implemented the push, pop, peek, isEmpty, and size methods, and created a main class to test the stack. We’ve also discussed the stack operations and example use cases. By following these steps, you can create a stack in Java and use it to implement various algorithms and data structures.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

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

Scroll to Top