π¦ Stack Implementation Using Arrays (Push, Pop, Peek, isEmpty, isFull & Display)
Welcome to Part 2 of our Stack in Java series.
In Part 1, we learned:
- β What is a Stack?
- β LIFO (Last In, First Out)
- β Stack Terminologies
- β Push, Pop, Peek
- β Overflow & Underflow
- β Applications of Stack
- β Advantages & Disadvantages
Now it’s time to implement a Stack using Arrays.
This is the most common implementation used in schools, colleges, interviews, and competitive programming.
By the end of this tutorial, you’ll be able to build a complete Stack in Java from scratch.
π Table of Contents
- Why Use Arrays?
- Stack Representation
- Stack Variables
- Creating a Stack
- Push Operation
- Pop Operation
- Peek Operation
- isEmpty()
- isFull()
- Display Stack
- Complete Java Program
- Dry Runs
- Time Complexity
- Common Mistakes
- FAQs
- Conclusion
π€ Why Implement Stack Using Arrays?
An array is one of the easiest ways to implement a stack.
Advantages:
- β Simple to understand
- β Fast implementation
- β Direct indexing
- β Excellent performance
However,
Arrays have one limitation.
Their size is fixed.
Suppose
Size = 5
Once all five locations are occupied,
no additional element can be inserted.
This causes Stack Overflow.
π¨ Stack Representation
Suppose
Size = 5
Initially
Index
0
1
2
3
4
Array
[ ][ ][ ][ ][ ]
Top
-1
A value of -1 means
the stack is empty.
After pushing
10
20
30
Array
Index
0 1 2 3 4
10 20 30
Top
2
π§ Understanding the Top Variable
The Top variable stores the index of the topmost element.
Initially
top = -1
Stack Empty
Push 10
top = 0
Push 20
top = 1
Push 30
top = 2
Current Stack
Top
β
30
20
10
π» Creating a Stack
We need three variables.
int stack[];
int top;
int size;
stack[]
Stores all elements.
top
Stores the index of the top element.
size
Stores the maximum capacity.
π» Constructor
class Stack {
int stack[];
int top;
int size;
Stack(int size){
this.size = size;
stack = new int[size];
top = -1;
}
}
Initially
Top = -1
β Push Operation
Push means
Insert an element into the stack.
Example
Current
30
20
10
Push
40
Result
40
30
20
10
π Algorithm
- Check Overflow.
- Increase Top.
- Store element.
π¨ Dry Run
Initially
Top = 2
Push
40
Increment
Top = 3
Store
stack[3] = 40
Done.
π» Java Program β Push
public void push(int value){
if(top == size - 1){
System.out.println("Stack Overflow");
return;
}
top++;
stack[top] = value;
}
β Pop Operation
Pop removes the top element.
Current
40
30
20
10
Pop
40
Result
30
20
10
π Algorithm
- Check Underflow.
- Read top element.
- Decrease Top.
π¨ Dry Run
Current
Top = 3
Pop
40
Decrease
Top = 2
Done.
π» Java Program β Pop
public int pop(){
if(top == -1){
System.out.println("Stack Underflow");
return -1;
}
int value = stack[top];
top--;
return value;
}
π Peek Operation
Peek returns the top element
without removing it.
Current
40
30
20
10
Peek
40
Stack remains unchanged.
π» Java Program β Peek
public int peek(){
if(top == -1){
System.out.println("Stack is Empty");
return -1;
}
return stack[top];
}
β isEmpty()
Checks whether the stack is empty.
π» Java Program
public boolean isEmpty(){
return top == -1;
}
β isFull()
Checks whether the stack is full.
π» Java Program
public boolean isFull(){
return top == size - 1;
}
π Display Operation
Displays every element.
Always start from Top.
Current
40
30
20
10
Output
40
30
20
10
π» Java Program β Display
public void display(){
if(top == -1){
System.out.println("Stack is Empty");
return;
}
for(int i = top; i >= 0; i--)
System.out.println(stack[i]);
}
π» Complete Java Program
class Stack {
int stack[];
int top;
int size;
Stack(int size){
this.size = size;
stack = new int[size];
top = -1;
}
public void push(int value){
if(top == size - 1){
System.out.println("Stack Overflow");
return;
}
stack[++top] = value;
}
public int pop(){
if(top == -1){
System.out.println("Stack Underflow");
return -1;
}
return stack[top--];
}
public int peek(){
if(top == -1){
System.out.println("Stack is Empty");
return -1;
}
return stack[top];
}
public boolean isEmpty(){
return top == -1;
}
public boolean isFull(){
return top == size - 1;
}
public void display(){
if(top == -1){
System.out.println("Stack is Empty");
return;
}
System.out.println("\nStack Elements:");
for(int i = top; i >= 0; i--)
System.out.println(stack[i]);
}
public static void main(String args[]){
Stack s = new Stack(5);
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.display();
System.out.println("\nTop Element = " + s.peek());
System.out.println("\nDeleted = " + s.pop());
s.display();
System.out.println("\nIs Empty? " + s.isEmpty());
System.out.println("Is Full? " + s.isFull());
}
}
Output
Stack Elements:
40
30
20
10
Top Element = 40
Deleted = 40
Stack Elements:
30
20
10
Is Empty? false
Is Full? false
π Time Complexity
| Operation | Time Complexity |
|---|---|
| Push | O(1) |
| Pop | O(1) |
| Peek | O(1) |
| isEmpty | O(1) |
| isFull | O(1) |
| Display | O(n) |
πΎ Space Complexity
Suppose the stack contains n elements.
Memory required
O(n)
β οΈ Common Mistakes
β Forgetting Overflow Check
Never write
stack[++top] = value;
without checking
top == size - 1
Otherwise,
an ArrayIndexOutOfBoundsException may occur.
β Forgetting Underflow Check
Never perform
top--;
when
top == -1
β Starting Display from Index 0
Wrong
for(int i=0;i<=top;i++)
Although it prints all elements, it prints them from bottom to top.
A stack should be displayed from Top to Bottom.
Correct
for(int i=top;i>=0;i--)
β Initializing Top to 0
Wrong
top = 0;
Correct
top = -1;
β Frequently Asked Questions
Q1. Why is Top initialized to -1?
Because there are no elements initially.
Q2. Why is Push O(1)?
Only one element is inserted at the top.
Q3. Why is Pop O(1)?
Only the top element is removed.
Q4. What causes Stack Overflow?
Trying to insert into a full array-based stack.
Q5. What causes Stack Underflow?
Trying to remove an element from an empty stack.
Q6. What is the limitation of an array-based stack?
Its size is fixed and cannot grow dynamically.
π― Key Takeaways
- π¦ Arrays provide a simple way to implement a Stack.
- π The Top variable keeps track of the current top element.
- β Push inserts an element in O(1) time.
- β Pop removes the top element in O(1) time.
- π Peek returns the top element without removing it.
- β οΈ Always check for Overflow and Underflow.
- π Display should always print elements from Top to Bottom.
π Conclusion
Implementing a Stack using Arrays is simple, efficient, and widely used in academic courses and programming interviews. The array-based implementation provides constant-time insertion and deletion at the top, making it an excellent choice when the maximum stack size is known in advance.
However, the major limitation of this approach is the fixed size of the array. If the stack becomes full, no additional elements can be inserted unless a larger array is created.
π In Part 3, we’ll overcome this limitation by implementing a Stack using a Linked List, where the stack grows dynamically without worrying about overflow due to fixed capacity. You’ll learn:
- π Stack using Linked List
- π¦ Node Structure
- β Push Operation
- β Pop Operation
- π Peek Operation
- β isEmpty()
- π Display Operation
- π» Complete Java Program
- π Dry Runs
- π Time Complexity Analysis
- βοΈ Advantages over Array Implementation