Stack Basics in C++
Basic Stack Operations in C++.
BeginnerSTL Containers ProgramsExample 7 of 10
stack-basics.cpp
1#include <iostream>2#include <stack>3using namespace std;45int main() {6 // Create stack7 stack<int> st;89 // Push elements10 st.push(10);11 st.push(20);12 st.push(30);13 st.push(40);14 st.push(50);1516 cout << "Stack size: " << st.size() << endl;17 cout << "Top element: " << st.top() << endl;1819 // Display stack (by popping)20 cout << "\nStack elements (LIFO - Last In First Out):" << endl;21 while (!st.empty()) {22 cout << st.top() << " ";23 st.pop();24 }25 cout << endl;2627 // Stack for string reversal28 stack<char> charStack;29 string str = "Hello";3031 cout << "\nOriginal string: " << str << endl;3233 // Push all characters34 for (char c : str) {35 charStack.push(c);36 }3738 // Pop to reverse39 string reversed = "";40 while (!charStack.empty()) {41 reversed += charStack.top();42 charStack.pop();43 }4445 cout << "Reversed string: " << reversed << endl;4647 // Check balanced parentheses48 stack<char> parenStack;49 string expression = "((()))";50 bool balanced = true;5152 for (char c : expression) {53 if (c == '(') {54 parenStack.push(c);55 } else if (c == ')') {56 if (parenStack.empty()) {57 balanced = false;58 break;59 }60 parenStack.pop();61 }62 }6364 if (parenStack.empty() && balanced) {65 cout << "\nExpression '" << expression << "' is balanced" << endl;66 } else {67 cout << "\nExpression '" << expression << "' is not balanced" << endl;68 }6970 return 0;71}
Output
Stack size: 5 Top element: 50 Stack elements (LIFO - Last In First Out): 50 40 30 20 10 Original string: Hello Reversed string: olleH Expression '((()))' is balanced
What's going on
The program demonstrates stack operations:
Creating stack and pushing elements
Accessing top element
Popping elements (LIFO order)
String reversal using stack
Balanced parentheses checking
Stacks provide simple, efficient last-in-first-out data access.
Header Files Used
1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <stack>
Provides stack container class.
Understanding Stack
LIFO Concept
:
Last In First Out
Last element added is first removed
Like a stack of plates
Only top element accessible
Key Features
:
Simple operations
O(1) time complexity
Last-in-first-out order
Top element only accessible
Stack Operations
push()
:
st.push(10); // Add to top
pop()
:
st.pop(); // Remove from top
top()
:
int value = st.top(); // Access top element
How it works
:
push(): adds element to top
pop(): removes top element
top(): returns top element (doesn't remove)
All O(1) operations
Stack Properties
Size and Empty
:
st.size() // Number of elements
st.empty() // True if empty
Access
:
Only top element accessible
Cannot access middle elements
Must pop to access lower elements
LIFO order enforced
String Reversal
Using Stack
:
1.Push all characters to stack
2.Pop characters to reverse string
How it works
:
Push: "Hello" → stack: o, l, l, e, H (top)
Pop: "olleH" (reversed)
LIFO naturally reverses order
Balanced Parentheses
Algorithm
:
1.Push '(' onto stack
2.Pop when ')' encountered
3.Balanced if stack empty at end
How it works
:
'(' pushes, ')' pops
Stack tracks nesting level
Empty stack = balanced
Non-empty = unbalanced
When to Use Stack
Best For
:
Expression evaluation
Balanced parentheses/braces
Function call simulation
Undo operations
Reverse order processing
Depth-first search (DFS)
Example Scenarios
:
Calculator (postfix evaluation)
Syntax checking
Backtracking algorithms
Recursion simulation
Browser back button
Important Considerations
LIFO Order
:
Last added = first removed
Cannot access arbitrary elements
Must pop to access lower elements
Natural for reverse processing
Performance
:
All operations: O(1)
Very efficient
Simple implementation
Minimal overhead
Limitations
:
Only top element accessible
Cannot search or access middle
Must pop to access other elements
Use other containers if random access needed
return 0;
This ends the program successfully.