To implement a stack data structure using arrays in C++ and perform basic operations such as push, pop, peek, and display.
- To understand the concept of a stack as a linear data structure.
- To learn how to use arrays and classes in C++ for implementing abstract data types.
- To practice error handling for stack overflow and underflow conditions.
- To develop logic-building skills by writing step-by-step algorithms.
- To understand how LIFO (Last In, First Out) principle is applied in computing.
A stack is a linear data structure that follows LIFO (Last In, First Out):
- The element inserted last is removed first.
- Example: Stack of books – only the top book can be removed or added.
- Push → Insert an element at the top.
- Pop → Remove the top element.
- Peek/Top → View the top element without removing it.
- Display → Show all elements in the stack.
- Undo/Redo operations in editors.
- Backtracking in puzzles (e.g., maze solving).
- Function calls in programming (call stack).
- Browser history (Back button).
- Overflow → Stack is full and cannot insert new elements.
- Underflow → Stack is empty and cannot remove/peek elements.
| Feature | Array-based Stack | Linked-list-based Stack |
|---|---|---|
| Memory Allocation | Fixed size (MAX_SIZE) | Dynamic, grows/shrinks |
| Speed | Fast (direct indexing) | Slightly slower (pointer ops) |
| Memory Usage | May waste space | Exact memory but extra for ptr |
| Implementation | Simple and easy | More complex |
push()
- Increment
topby 1. - Assign the value to
array[top]. - Return the inserted element.
Main Program
- Initialize integer array
array[5]andtop = -1. - Use a loop to push elements 5 times.
- Print inserted elements.
pushElement(num)
- Check if
topIndex == MAX_SIZE - 1. - If yes → print
STACK OVERFLOW. - Else → increment
topIndexand insertnum.
popElement()
- If
topIndex == -1→ printSTACK UNDERFLOW. - Else → return
arr[topIndex]and decrementtopIndex.
peekTop()
- If
topIndex == -1→ printSTACK UNDERFLOW. - Else → return
arr[topIndex].
displayStack()
- If
topIndex == -1→ printSTACK UNDERFLOW. - Else → print all elements from index 0 to
topIndex.
- Uses
array[5]andtopto track the stack. push()inserts value 77 at the top.- Main function calls
push()5 times. - Uses pass-by-reference for top to update the main stack.
- Private data:
arr[MAX_SIZE]→ Stores stack elements.topIndex→ Tracks top element.
- Public functions:
pushElement(int num)→ Insert element.popElement()→ Remove top element.peekTop()→ View top element.displayStack()→ Display all elements.
- Main function demonstrates push, pop, peek, display operations.
- Class & Object → Encapsulation of stack operations.
- Array → Stores elements sequentially.
- Encapsulation → Access stack via methods only.
- Conditionals (if-else) → Handle overflow/underflow.
- Loops (for loop) → Display stack elements.
- Macros (#define) → For constants like
MAX_SIZEandERROR_CODE.
- Successfully implemented a stack using arrays in C++.
- Demonstrated push, pop, peek, display operations.
- Showed array-based vs linked-list-based stack differences.
- Improved understanding of data structures, OOP, and error handling.
- Stack is fundamental in compilers, operating systems, and problem-solving applications.