Java Program to Implement Stack Using Array
push() to add elements, pop() to remove elements, and display() to show stack contents.Examples
How to Think About It
Algorithm
Code
public class Stack { private int[] arr; private int top; private int capacity; public Stack(int size) { arr = new int[size]; capacity = size; top = -1; } public void push(int x) { if (top == capacity - 1) { System.out.println("Stack Overflow - Cannot push " + x); return; } arr[++top] = x; System.out.println("Pushed " + x); } public void pop() { if (top == -1) { System.out.println("Stack Underflow - No elements to pop"); return; } System.out.println("Popped " + arr[top--]); } public void display() { if (top == -1) { System.out.println("Stack is empty"); return; } System.out.print("Stack elements: "); for (int i = 0; i <= top; i++) { System.out.print(arr[i] + " "); } System.out.println(); } public static void main(String[] args) { Stack stack = new Stack(3); stack.push(10); stack.push(20); stack.pop(); stack.display(); } }
Dry Run
Let's trace pushing 10, pushing 20, popping, and displaying the stack.
Initialize stack
Array: [_, _, _], top = -1
Push 10
top increments to 0, arr[0] = 10
Push 20
top increments to 1, arr[1] = 20
Pop element
Element at arr[1] = 20 removed, top decrements to 0
Display stack
Print elements from arr[0] to arr[0]: 10
| Operation | top | Array contents |
|---|---|---|
| Initialize | -1 | [_, _, _] |
| Push 10 | 0 | [10, _, _] |
| Push 20 | 1 | [10, 20, _] |
| Pop | 0 | [10, 20, _] |
| Display | 0 | [10, 20, _] |
Why This Works
Step 1: Tracking top index
The top variable keeps track of the last filled position in the array, starting at -1 when empty.
Step 2: Push operation
When pushing, top increases by 1 and the new element is stored at that index, ensuring LIFO order.
Step 3: Pop operation
Popping removes the element at top and then decreases top, so the stack shrinks from the top.
Alternative Approaches
class StackNode { int data; StackNode next; StackNode(int d) { data = d; } } class Stack { StackNode top; void push(int x) { StackNode node = new StackNode(x); node.next = top; top = node; System.out.println("Pushed " + x); } void pop() { if (top == null) { System.out.println("Stack Underflow"); return; } System.out.println("Popped " + top.data); top = top.next; } void display() { if (top == null) { System.out.println("Stack is empty"); return; } System.out.print("Stack elements: "); StackNode temp = top; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } }
import java.util.Stack; public class Main { public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); stack.push(10); stack.push(20); System.out.println("Popped " + stack.pop()); System.out.println("Stack elements: " + stack); } }
Complexity: O(1) time, O(n) space
Time Complexity
Push and pop operations take constant time O(1) because they only update the top index and array element.
Space Complexity
The stack uses O(n) space where n is the capacity of the array, storing all elements in a fixed-size array.
Which Approach is Fastest?
Array-based stack is fastest for fixed size due to direct indexing; linked list stack is flexible but slightly slower due to node allocation.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Array-based Stack | O(1) | O(n) | Fixed size, fast access |
| Linked List Stack | O(1) | O(n) | Dynamic size, flexible memory |
| Java Built-in Stack | O(1) | O(n) | Quick use, less control |