A stack is a LIFO (Last In, First Out) abstract data type where all insertions and deletions happen at the same end, called the top. Think of a stack of plates: you always add to the top and remove from the top. Stacks are used everywhere in computing — from function call management to expression evaluation and undo functionality.
In this post, we implement a stack in Java using a fixed-size integer array, with push, pop, peek, and display operations accessible through a menu-driven console program.
Stack Operations
- push — Adds an element to the top of the stack. Fails with overflow if the stack is full.
- pop — Removes and returns the top element. Fails with underflow if the stack is empty.
- peek — Returns the top element without removing it.
- display — Prints all elements from bottom to top.
Java Program: Stack Implementation
import java.io.*;
// Array-based stack storing integer values
class Stack {
int capacity; // Maximum number of elements the stack can hold
int item[]; // Backing array
int top; // Index of the topmost element; -1 means empty
public Stack() {
capacity = 10;
item = new int[capacity];
top = -1; // Stack starts empty
}
// Pushes a value onto the top of the stack
public void push(int element) {
if (top == (capacity - 1)) {
System.out.println("Stack Overflow - stack is full");
} else {
top++;
item[top] = element;
}
}
// Removes and returns the top element
public int pop() {
if (top == -1) {
System.out.println("Stack Underflow - stack is empty");
return -1;
} else {
int value = item[top];
top--;
return value;
}
}
// Returns the top element without removing it
public int peek() {
if (top == -1) {
System.out.println("Stack is empty - nothing to peek");
return -1;
} else {
return item[top];
}
}
// Displays all elements from bottom (index 0) to top
public void display() {
if (top == -1) {
System.out.println("Stack is empty");
} else {
System.out.print("Stack (bottom to top): ");
for (int i = 0; i <= top; i++) {
System.out.print(item[i] + " ");
}
System.out.println();
}
}
}
public class StackDemo {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Stack stack = new Stack();
int choice;
do {
System.out.println();
System.out.println("--- Stack Implementation Menu ---");
System.out.println("1. Push (add element)");
System.out.println("2. Pop (remove element)");
System.out.println("3. Peek (view top element)");
System.out.println("4. Display stack");
System.out.println("5. Exit");
System.out.print("Select option: ");
choice = Integer.parseInt(reader.readLine());
switch (choice) {
case 1:
System.out.print("Enter number to push: ");
int element = Integer.parseInt(reader.readLine());
stack.push(element);
break;
case 2:
int popped = stack.pop();
if (popped != -1) System.out.println("Popped element: " + popped);
break;
case 3:
int topVal = stack.peek();
if (topVal != -1) System.out.println("Top element: " + topVal);
break;
case 4:
stack.display();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid option");
}
} while (choice != 5);
}
}
How the Code Works
- Stack class — Uses an integer array of size 10 and a
topvariable initialised to-1to indicate an empty stack. - push() — Checks if
top == capacity - 1(overflow). If not, incrementstopand stores the element atitem[top]. - pop() — Checks if
top == -1(underflow). If not, savesitem[top], decrementstop, and returns the saved value. - peek() — Returns
item[top]without changingtop. Safe preview of the next element to be popped. - display() — Loops from index 0 to
top, printing all elements from bottom to top of the stack.
Sample Output
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 1
Enter number to push: 10
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 4
Stack (bottom to top): 10
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 1
Enter number to push: 20
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 4
Stack (bottom to top): 10 20
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 1
Enter number to push: 30
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 4
Stack (bottom to top): 10 20 30
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 3
Top element: 30
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 2
Popped element: 30
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 4
Stack (bottom to top): 10 20
--- Stack Implementation Menu ---
1. Push (add element)
2. Pop (remove element)
3. Peek (view top element)
4. Display stack
5. Exit
Select option: 5
Exiting...
Output Explanation
- After pushing 10, 20, 30 — display shows them in push order (bottom to top).
- Peek returns 30 (the most recently pushed element) without removing it.
- Pop removes 30 first (LIFO), then 20, leaving only 10 in the stack.
See Also
- Implementation of Stack using Linked List in Java — Dynamic stack with no fixed capacity limit
- Java Program to Convert Infix Notation to PostFix Notation — Classic stack application
- Evaluating Postfix Expression with Java — Another real-world use of stack
- Java Implementation of Queue using Linked List — The FIFO counterpart to stack
Conclusion
The array-based stack is the simplest and most cache-friendly stack implementation. Its O(1) push and pop operations make it highly efficient. The main limitation is the fixed capacity — if you need an unbounded stack, see the linked list-based implementation linked above. Stacks are one of the most widely used data structures, appearing in compiler design, function call management, browser history, and expression evaluation.