Stack Data Structure in C++: LIFO Principles & Code Examples
Introduction
A stack is a linear data structure in C++ that follows the LIFO (Last In, First Out) principle, it means the element that is inserted last is the first one to be removed. Stacks are commonly used in programming for tasks such as function calls, expression evaluation, undo/redo operations, and backtracking. It can be implemented using arrays, linked lists, or by using the built-in stack container from the Standard Template Library (STL). Understanding stacks helps beginners learn how data is managed and accessed in a structured and efficient way.
Let’s Explore Stack Data Structure in C++
Today, we are going to use Stacks in C++. We can make using either by using arrays or linked lists. A stack in C++ is a data structure that follows the Last In, First Out (LIFO) rule. This means the last element added is the first one removed, like a stack of books.
In C++, stacks are commonly used through the STL std::stack. You can add elements using push(), remove the top element using pop(), and view the top element using top(). Always check empty() before accessing the stack.
Stacks can also be implemented using arrays or vectors to understand how they work internally. They are used in function calls, undo/redo operations, expression evaluation, and checking parentheses.
Here are two short and simple stack implementations in C++:
Stack using Array
#include <iostream>
using namespace std;
#define MAX 5
class Stack {
int arr[MAX];
int top;
public:
Stack() { top = -1; }
void push(int x) {
if (top == MAX - 1) return;
arr[++top] = x;
}
void pop() {
if (top == -1) return;
top--;
}
int peek() {
return arr[top];
}
};
Now, here's the overall another code to understand it better:As shown in the above image, this program implements a stack using a fixed-size array. The variable top keeps track of the index of the top element. When an element is pushed, top is increased and the value is stored in the array. When an element is popped, top is decreased, removing the top element. If the stack is full, it shows overflow, and if it is empty, it shows underflow.Stack using Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() { top = NULL; }
void push(int x) {
Node* n = new Node();
n->data = x;
n->next = top;
top = n;
}
void pop() {
if (top == NULL) return;
Node* temp = top;
top = top->next;
delete temp;
}
int peek() {
return top->data;
}
};
Now, here's the overall another code to understand it better:
As shown in the above image, this program implements a stack using a linked list. Each new element is added at the beginning of the list, and the pointertop always points to the top node. Pushing creates a new node and links it to the previous top. Popping removes the top node and frees its memory. This stack grows dynamically, so there is no fixed size limit.Both follow LIFO and support push, pop, and peek.
Important Notes (Things Beginners Often Miss with Stacks in C++)
One common mistake beginners make while working with stacks is ignoring overflow and underflow conditions. When using an array-based stack, pushing elements beyond the fixed size causes overflow, and popping from an empty stack causes underflow. Always check conditions like top == MAX - 1 for overflow and top == -1 for underflow to keep the program safe.
Another frequent issue is accessing the top element without checking if the stack is empty. Calling top() or peek() on an empty stack can lead to undefined behavior or runtime errors. Using empty() (in STL stacks) or checking the top pointer/index before accessing elements is very important.
Beginners also sometimes confuse stacks with other data structures like queues or arrays. Remember that a stack strictly follows LIFO (Last In, First Out), meaning you can only insert and remove elements from one end, called the top. There is no direct access to middle elements.
In linked-list-based stacks, a common mistake is forgetting to free memory when popping elements. If delete is not used properly, memory leaks can occur. Good memory management is especially important in dynamic implementations.
Lastly, many learners underestimate how often stacks are used internally. Function calls, recursion, expression evaluation, and backtracking all rely on stacks behind the scenes. Understanding stack behavior makes it much easier to grasp recursion, compiler working, and advanced algorithms later on. With practice, stacks become one of the simplest yet most powerful tools in C++ programming.
Conclusion
Stacks play an important role in solving many real-world and programming problems because their simple LIFO behavior makes them easy to understand and implement, yet very powerful in practice. By learning stacks in C++, beginners gain a deeper understanding of data flow, memory usage, and algorithm design. Mastering stacks also prepares you to explore more advanced data structures and concepts such as recursion, expression parsing, and depth-first search.
Comments
Post a Comment