How to Implement Stack Using Array: Simple Guide
To implement a
stack using an array, use an array to store elements and a variable to track the top position. You add elements with push by placing them at the top index and remove with pop by decreasing the top index, following Last-In-First-Out (LIFO) order.Syntax
A stack using an array typically has these parts:
- Array: Holds the stack elements.
- Top index: Tracks the current top element position.
- Push operation: Adds an element at the top and increments the top index.
- Pop operation: Removes the top element and decrements the top index.
- Peek operation: Returns the top element without removing it.
java
class Stack { int[] arr; int top; int capacity; Stack(int size) { arr = new int[size]; capacity = size; top = -1; // stack is empty } void push(int x) { if (top == capacity - 1) { System.out.println("Stack Overflow"); return; } arr[++top] = x; } int pop() { if (top == -1) { System.out.println("Stack Underflow"); return -1; // or throw exception } return arr[top--]; } int peek() { if (top == -1) { System.out.println("Stack is empty"); return -1; } return arr[top]; } }
Example
This example shows how to create a stack, push elements, pop an element, and peek at the top element.
java
class Stack { int[] arr; int top; int capacity; Stack(int size) { arr = new int[size]; capacity = size; top = -1; } void push(int x) { if (top == capacity - 1) { System.out.println("Stack Overflow"); return; } arr[++top] = x; } int pop() { if (top == -1) { System.out.println("Stack Underflow"); return -1; } return arr[top--]; } int peek() { if (top == -1) { System.out.println("Stack is empty"); return -1; } return arr[top]; } public static void main(String[] args) { Stack stack = new Stack(3); stack.push(10); stack.push(20); stack.push(30); System.out.println("Top element is: " + stack.peek()); System.out.println("Popped element: " + stack.pop()); System.out.println("Top element after pop: " + stack.peek()); } }
Output
Top element is: 30
Popped element: 30
Top element after pop: 20
Common Pitfalls
Common mistakes when implementing a stack using an array include:
- Not checking for stack overflow before pushing, which causes errors when the array is full.
- Not checking for stack underflow before popping, which causes errors when the stack is empty.
- Incorrectly updating the
topindex, leading to wrong element access or skipping elements.
Always validate the top index before push and pop operations.
java
/* Wrong way: No overflow check */ void push(int x) { arr[++top] = x; // May cause error if top >= capacity } /* Correct way: Check overflow */ void push(int x) { if (top == capacity - 1) { System.out.println("Stack Overflow"); return; } arr[++top] = x; }
Quick Reference
Remember these key points when implementing a stack with an array:
- Top starts at -1 to indicate an empty stack.
- Push: Increment top, then insert element.
- Pop: Return element at top, then decrement top.
- Overflow: Happens if top reaches array size - 1.
- Underflow: Happens if top is -1 when popping.
Key Takeaways
Use an array and a top index to track the stack's current element.
Always check for overflow before pushing and underflow before popping.
Push adds an element at top+1; pop removes the element at top.
Top index starts at -1 to represent an empty stack.
Peek returns the top element without removing it.