Stack in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Stacks are one of the most useful data structures in programming. One of the fundamental techniques used for problem-solving is recursion. Whenever a function is called the memory is allocated to it on a stack.

When this function calls itself a new memory is allocated and is placed on the memory of the calling function. This process is executed and understood with the knowledge of stacks.

They find numerous applications in real life problems.

  • Mechanism of opening and closing of webpages in a browser
  • Wearing/removing of bangles
  • Stack of books/plates
  • Undo and redo mechanism of text editors
  • Space for parameters and local variables is created internally using a stack in a program.
  • The compiler’s syntax check for matching braces is implemented by using stack.
  • Stacks enable us in faster parsing of expressions for evaluation using prefix and postfix expressions

Stacks follow a certain order for adding and removing data (LIFO).

What is Stack in Java?

A stack is a linear data structure. It provides a container to store data items which can be accessed from one side of the container only.

Here the element that is inserted at last is deleted first.

This is called the Last-In-First-Out (LIFO) principle.

It means that we can only access the latest entered elements.

LIFO Method of Stack Class in Java

Stack in Java

Java Stack Implementation

The concept of stacks can be implemented in multiple ways.

In Java, we can implement stacks in the following ways:

1. Stacks can be implemented using arrays

Java Stack Implementation using Arrays

  • We declare an empty array of a specific size in this process.
  • We declare a variable top to constantly point to the top element of the stack. Initially, it is equal to -1, which denotes underflow (the stack is empty and hence we cannot delete anything).
  • When we want to add an element to the stack, we first increment top and then add the element at the index top.
  • When we want to remove the top element, we decrease top by 1.
  • When top equals the array size, it denotes overflow (the stack is filled and we cannot add a new value).

2. Stacks can be implemented using linked list

Java Stack Implementation using Linked List

  • We create a node pointer named head to point to the top of the stack.
  • We initialize head with null to denote an empty stack.
  • When we want to add an element, we update head with the new element's node address. We make the new node's next pointer point to the previous address stored in head.
  • We update head with the new node's address.
  • When we want to remove the top element, we simply update the head pointer with the address that the top element points to.

Stack class in Java

Stacks can be implemented using the Stack class provided to us by the Java package.

  • Here we import the Stack class from the util package in Java, and then implement it with data types like Integer, String, Char, etc.
  • We can also implement stacks for user-defined data types.

Creating a Java stack

For implementing stacks using the Stack class provided by the java packages we need to first import a ‘util’ package.

Syntax

After importing the stack class we need the syntax to declare the stack variable and dynamically allocate memory for it.

Syntax

Output:

Java Stack Methods

1) push()

This method inserts a given value into the stack at the top

Output:

2) pop()

This method removes the top element from the stack and returns it until the stack becomes completely empty

Output:

3) peek()

This method returns us the top element of the stack. It throws an error when we use it with an empty stack and tells us underflow.

(Underflow : Condition where our stack becomes empty)

Output:

4) search()

This method is used to know whether an element exists in our stack or not.

If the element exists it returns the position of that element from the top of the stack.

If the element is absent it returns -1.

Output:

5) empty()

This method returns a boolean value.

If the stack is empty it returns true otherwise it returns false.

Output:

Conclusion

  • A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, where the last element inserted is the first one to be removed.
  • Stacks can be implemented using different approaches, such as arrays or linked lists. Java provides the Stack class in the java.util package, which simplifies stack implementation and supports various data types, including user-defined types.
  • The article explains essential stack methods, including:
    • push() for inserting elements
    • pop() for removing and returning the top element
    • peek() for accessing the top element without removal
    • search() for finding the position of an element
    • empty() for checking if the stack is empty.