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;
4
5int main() {
6 // Create stack
7 stack<int> st;
8
9 // Push elements
10 st.push(10);
11 st.push(20);
12 st.push(30);
13 st.push(40);
14 st.push(50);
15
16 cout << "Stack size: " << st.size() << endl;
17 cout << "Top element: " << st.top() << endl;
18
19 // 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;
26
27 // Stack for string reversal
28 stack<char> charStack;
29 string str = "Hello";
30
31 cout << "\nOriginal string: " << str << endl;
32
33 // Push all characters
34 for (char c : str) {
35 charStack.push(c);
36 }
37
38 // Pop to reverse
39 string reversed = "";
40 while (!charStack.empty()) {
41 reversed += charStack.top();
42 charStack.pop();
43 }
44
45 cout << "Reversed string: " << reversed << endl;
46
47 // Check balanced parentheses
48 stack<char> parenStack;
49 string expression = "((()))";
50 bool balanced = true;
51
52 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 }
63
64 if (parenStack.empty() && balanced) {
65 cout << "\nExpression '" << expression << "' is balanced" << endl;
66 } else {
67 cout << "\nExpression '" << expression << "' is not balanced" << endl;
68 }
69
70 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.