Aim: To study the concept of Stack in C++ and implement operations like push, pop, and display.
Tools: VS Code.
Stack is a fundamental linear data structure that follows the Last In First Out (LIFO) principle. It is one of the most important and widely used data structures in computer science, serving as the foundation for many algorithms and system-level operations.
-
The last element inserted is the first one to be removed.
-
It has two main operations:
- Push – add an element to the top of the stack.
- Pop – remove the top element from the stack.
-
Can be implemented using arrays or linked lists.
-
Requires a pointer/index (top) to keep track of the top element.
class Stack {
private:
int arr[MAXSIZE]; // Array-based implementation
int top; // Index of top element
public:
Stack() : top(-1) {} // Initialize empty stack
void push(int data) {
if (top < MAXSIZE - 1) {
arr[++top] = data;
} else {
cout << "Stack Overflow" << endl;
}
}
void pop() {
if (top >= 0) {
top--;
} else {
cout << "Stack Underflow" << endl;
}
}
};
-
Array-based Stack: Fixed size, fast access, memory efficient.
-
Linked List Stack: Dynamic size, flexible memory allocation.
-
Template Stack: Generic implementation for any data type.
-
STL Stack: Standard library implementation using containers.
-
Function Call Management: Managing function calls and returns
-
Expression Evaluation: Infix to postfix conversion, arithmetic expressions
-
Syntax Parsing: Compiler design, bracket matching, syntax analysis
-
Memory Management: Stack memory allocation for local variables
A Stack class is created with an array to store elements and an integer top to track the top position.push() adds a new element at the top of the stack. pop() removes the top element. display() traverses and prints all stack elements. Overflow and underflow conditions are handled using checks on top.
-
Start
-
Define a class Stack with array and top index.
-
Initialize top = -1 (empty stack).
-
Push Operation:
-
Check if top >= MAX-1 → Overflow
-
Else increment top and insert value
-
Pop Operation:
-
Check if top < 0 → Underflow
-
Else remove element and decrement top
-
Display Operation:
-
Traverse array from 0 to top and print elements
-
In main(), perform multiple push and pop operations.
-
End
Stacks are simple and useful for tasks like expression evaluation, function call tracking, and undo operations. Using arrays, we can efficiently implement stack operations while handling overflow and underflow conditions.