How to implement Stack Data Structure in Java Program
Stack Data Structure
An array or linked list can be used to implement a stack in Java. In the java.util package, Java includes a pre-implemented Stack class that extends the Vector class and implements the Stack interface.
- Push operation adds an element to the top of the stack
- Pop operation removes an element from the top of the stack
To know more about Java Program to Implement Stack Data Structure read the complete article.
Steps to Implement Stack Data Structure in Java Program
- Here are the steps to implement a stack data structure in Java:
- Create a class “Stack” to define the stack data structure.
- Define an array of integers or any other data type to store the elements in the stack.
- Create a variable “top” to keep track of the top element of the stack.
- Implement the push() method to add elements to the top of the stack.
- Implement the pop() method to remove elements from the top of the stack.
- Implement the isEmpty() method to check if the stack is empty.
- Implement the peek() method to get the top element of the stack without removing it.
- In the main method, create an object of the Stack class and test the methods by pushing and popping elements from the stack.
Stack Data Structure :
A Stack is a Last-In-First-Out (LIFO) data structure in Java, where elements are added and removed from the top of the stack. It is an abstract data type with two main operations: push and pop.
Java Generics :
Java Generics are eliminated at runtime, thus runtime access to generic type information is prevented, but the compiler still makes sure the code is type-safe.
Let’s look at a Java Program to Implement Stack Data Structure to perform certain operations.
Example 1: Java Program to Implement Stack Data Structure
Run
class Stack { int top; int[] stack = new int[100]; Stack() { top = -1; } public void push(int x) { stack[++top] = x; } public int pop() { return stack[top--]; } public boolean isEmpty() { return top == -1; } public int peek() { return stack[top]; } } public class Main { public static void main(String[] args) { Stack stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); while (!stack.isEmpty()) { System.out.println(stack.pop()); } } }
Output
3 2 1
Explanation:
- The code defines a class Stack which implements the stack data structure. The class has the following attributes:
- top: an integer variable to keep track of the top element of the stack. It is initialized to -1, which represents an empty stack.
- stack: an array of integers to store the elements in the stack. It has a capacity of 100 elements. The class has the following methods:
- push(int x): this method adds an element x to the top of the stack. It increments the top value before adding the element to the stack array.
- pop(): this method removes and returns the top element of the stack. It decrements the top value after removing the element from the stack array.
- isEmpty(): this method returns a boolean value indicating if the stack is empty or not. It returns true if top is equal to -1, which means the stack is empty.
- peek(): this method returns the top element of the stack without removing it.
- The Main class creates an object of the Stack class and tests the methods by pushing and popping elements from the stack. The main method pushes three elements to the stack and then removes and prints them until the stack is empty. This implementation of the stack data structure in Java can be used as a basic building block for more complex data structures and algorithms.
Example 2 : Java Program to Implement Stack
Run
import java.util.ArrayList; class Stack{ private ArrayList list = new ArrayList (); public void push(T element) { list.add(element); } public T pop() { if (list.isEmpty()) { throw new IllegalStateException("Stack is empty"); } return list.remove(list.size() - 1); } public boolean isEmpty() { return list.isEmpty(); } public T peek() { if (list.isEmpty()) { throw new IllegalStateException("Stack is empty"); } return list.get(list.size() - 1); } } public class Main { public static void main(String[] args) { Stack stack = new Stack<>(); stack.push(1); stack.push(2); stack.push(3); System.out.println("Peek: " + stack.peek()); System.out.println("Pop: " + stack.pop()); System.out.println("Pop: " + stack.pop()); System.out.println("Is Empty: " + stack.isEmpty()); System.out.println("Pop: " + stack.pop()); System.out.println("Is Empty: " + stack.isEmpty()); } }
Output
Peek: 3 Pop: 3 Pop: 2 Is Empty: false Pop: 1 Is Empty: true
Explanation:
This Java code implements a generic Stack class using an ArrayList as the underlying data structure. The stack has the following operations:
push: add an element to the top of the stack
pop: remove and return the top element from the stack
peek: return the top element from the stack without removing it
isEmpty: returns whether the stack is empty or not
The Main class tests the stack by creating a stack of integers, pushing 3 elements, printing the result of the peek and pop operations, and checking if the stack is empty after each pop. The output of the program would be:
Peek: 3
Pop: 3
Pop: 2
Is Empty: false
Pop: 1
Is Empty: true
Example 3: Java Program to sort a map by Values.
Run
import java.util.Stack; class Main { public static void main(String[] args) { Stack integerStack = new Stack<>(); // Pushing 5 element into the stack integerStack.push(79); integerStack.push(9); integerStack.push(7); integerStack.push(7979); integerStack.push(5); // printing the stack's elements System.out.println("Elements in the Stack :: " + integerStack); // Pop operation on the stack, 2 would be removed from the top integerStack.pop(); // printing the stack's elements System.out.println("Elements in the Stack :: " + integerStack); // Peek operation on the stack,100 would be printed System.out.println("Top of the stack :: " + integerStack.peek()); } }
Output
Elements in the Stack :: [79, 9, 7, 7979, 5] Elements in the Stack :: [79, 9, 7, 7979] Top of the stack :: 7979
Explanation:
- In the main method, the Stack class is used to create a stack of integers and perform a series of operations on it. The program pushes three integers onto the stack, prints the top of the stack and the stack size, and then pops all elements from the stack, printing each one as it is removed. Finally, the program prints that the stack is empty.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment