Stacks in C++
Stacks in C++
Stacks in C++
A stack is a data structure that follows the LIFO (Last In, First Out) principle, where the last element
added to the stack is the first one to be removed. Think of a stack as a pile of plates; the last plate you
place on top is the first one you take off.
In C++, stacks can be implemented using:
1. Using `std::stack`
The `std::stack` container is easy to use and provides built-in functionality for stack operations. It
requires the `<stack>` header file.
Operations:
Example:
cpp
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
std::cout << "Top element: " << s.top() << std::endl; // Output: 30
// Popping elements
s.pop();
std::cout << "Top element after pop: " << s.top() << std::endl; // Output: 20
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/8
// Checking if the stack is empty
if (s.empty()) {
std::cout << "Stack is empty" << std::endl;
} else {
std::cout << "Stack size: " << s.size() << std::endl; // Output: 2
}
return 0;
}
You can implement a stack using an array or a linked list to better understand its working.
Array-based Stack:
cpp
#include <iostream>
#define MAX 1000
class Stack {
int top;
int arr[MAX];
public:
Stack() { top = -1; }
int main() {
Stack s;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/8
s.push(10);
s.push(20);
s.push(30);
return 0;
}
cpp
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class Stack {
Node* top;
public:
Stack() { top = nullptr; }
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/8
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
return 0;
}
Key Points:
explain deeply
A stack is a linear data structure that works on the LIFO (Last In, First Out) principle. It allows elements
to be added and removed only at one end, called the top of the stack. This ensures that the last element
inserted is the first one removed.
Think of a real-life example: a stack of plates or books. You can only add a plate or book to the top, and
you can only remove the topmost one.
The C++ Standard Template Library (STL) provides a ready-to-use stack container. It simplifies stack
operations without the need to write custom implementations.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/8
Syntax:
cpp
#include <stack>
std::stack<int> s;
Operations:
Example:
cpp
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
s.pop(); // Remove 30
std::cout << "Top element after pop: " << s.top() << std::endl; // 20
std::cout << "Is stack empty? " << (s.empty() ? "Yes" : "No") << std::endl;
return 0;
}
1. Using Arrays
An array-based implementation uses a fixed-size array and a pointer/index (`top`) to track the last
element.
Key Points:
Implementation:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/8
cpp
#include <iostream>
#define MAX 1000 // Maximum stack size
class Stack {
int top; // Index of the top element
int arr[MAX]; // Array to store stack elements
public:
Stack() { top = -1; } // Initialize stack as empty
// Push operation
bool push(int x) {
if (top >= MAX - 1) { // Stack overflow check
std::cout << "Stack Overflow\n";
return false;
}
arr[++top] = x; // Increment top and add element
return true;
}
// Pop operation
int pop() {
if (top < 0) { // Stack underflow check
std::cout << "Stack Underflow\n";
return -1;
}
return arr[top--]; // Return top element and decrement
}
// Peek operation
int peek() {
if (top < 0) {
std::cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
return 0;
}
A linked list implementation of a stack allows dynamic resizing. Each node contains data and a pointer to
the next node.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/8
Key Points:
Implementation:
cpp
#include <iostream>
// Node class
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
// Stack class
class Stack {
Node* top; // Pointer to the top element
public:
Stack() { top = nullptr; } // Initialize as empty
// Push operation
void push(int x) {
Node* newNode = new Node(x);
newNode->next = top;
top = newNode;
}
// Pop operation
int pop() {
if (!top) { // Check for underflow
std::cout << "Stack Underflow\n";
return -1;
}
Node* temp = top;
top = top->next;
int popped = temp->data;
delete temp;
return popped;
}
// Peek operation
int peek() {
if (!top) {
std::cout << "Stack is Empty\n";
return -1;
}
return top->data;
}
int main() {
Stack s;
s.push(10);
s.push(20);
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/8
s.push(30);
return 0;
}
Applications of Stacks
1. Expression Evaluation:
Used for evaluating postfix and prefix expressions.
Helps in converting infix to postfix/prefix.
2. Undo Operations:
Used in text editors for "undo" functionality.
3. Backtracking:
Algorithms like maze solving and N-Queens use stacks.
4. Function Call Stack:
Manages function calls and local variables in programming languages.
5. Browser History:
Maintains back/forward navigation using stacks.
Let me know if you'd like a deeper dive into a specific area like applications, performance, or variations!
Is this conversation helpful so far?
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/8