How to Push and Pop Elements from a Stack in Java?

How to Push and Pop Elements from a Stack in Java?

Introduction

The Stack class in Java represents a collection of elements 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. The Stack class is part of the Java Collections Framework and extends the Vector class. In this tutorial, we will cover how to push and pop elements from a stack, along with code examples for a clear understanding.

Understanding Stack Operations in Java

In Java, the Stack class provides various methods for working with stacks. Two of the most fundamental operations are:

  • Push: Adds an element to the top of the stack.
  • Pop: Removes the element from the top of the stack.

Push Operation in Java Stack

The push() method is used to add an element to the stack. When you push an element, it is added to the top of the stack. Here’s a basic example demonstrating how to use the push() method:

Example: Using push() Method


import java.util.Stack;

public class StackPushExample {
    public static void main(String[] args) {
        // Create a Stack of integers
        Stack stack = new Stack<>();

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

        // Display the stack
        System.out.println("Stack after push operations: " + stack);
    }
}
      

In the above code, we create a Stack of integers and push three elements (10, 20, and 30) to it. The System.out.println() statement prints the contents of the stack.

Pop Operation in Java Stack

The pop() method is used to remove and return the element at the top of the stack. If the stack is empty, calling pop() will throw an EmptyStackException. Here’s how the pop operation works:

Example: Using pop() Method


import java.util.Stack;

public class StackPopExample {
    public static void main(String[] args) {
        // Create a Stack of integers
        Stack stack = new Stack<>();

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

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

        // Display the stack after pop operations
        System.out.println("Stack after pop operations: " + stack);
    }
}
      

In the above example, we push three elements (10, 20, and 30) to the stack and then pop two elements from the stack. Each pop operation removes the topmost element, and the stack is updated accordingly.

Additional Stack Methods

Besides push() and pop(), the Stack class provides other useful methods, such as:

  • peek(): Returns the top element of the stack without removing it.
  • empty(): Checks if the stack is empty.
  • search(): Searches for an element in the stack and returns its position from the top of the stack.

Example of Using peek() and empty() Methods

Here’s an example that demonstrates the use of peek() and empty() methods in Java Stack:

Example: Using peek() and empty() Methods


import java.util.Stack;

public class StackPeekEmptyExample {
    public static void main(String[] args) {
        // Create a Stack of integers
        Stack stack = new Stack<>();

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

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

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

In this example, the peek() method retrieves the top element without removing it, and empty() checks whether the stack is empty.

Advantages of Using Stack in Java

The Stack data structure provides a simple yet powerful way to manage data in a LIFO order. Some advantages of using a stack include:

  • Efficient implementation for undo operations in text editors.
  • Used for parsing expressions (infix, prefix, postfix).
  • Useful in depth-first search (DFS) algorithms.

Conclusion

In Java, the Stack class provides an easy and efficient way to perform stack-based operations. The push() and pop() methods are the core operations, allowing you to add and remove elements from the stack. Understanding how to use these methods will help you in solving many algorithmic problems that require LIFO order.

© 2024 Tech Interview Guide. All Rights Reserved.

Please follow and like us:

Leave a Comment