- Example of Stack
- Why Stack is Linear Data Structure ?
- LIFO Principle
- How is a stack implemented ?
- Why Operations are needed in a stack ?
- Stack Operations in Data Structure
- 1. Push Operation
- Code Snippet of PUSH Operation
- 2. Pop Operation
- Code Snippet of POP Operation
- 3. Peek Operation
- Code Snippet of PEEK Operation
- Time & Space Complexity
- Stack Applications in Data Structure
- Conclusion
A stack is a type of linear data structure where elements are added and removed only from one end, called the top. Because of this, a stack follows the LIFO (Last-In-First-Out) principle, which means the last element added is the first one to be removed, and the first element added will be removed last.
A simple example is a pile of plates. When you place plates on top of each other, the last plate you put on the pile is the first one you take off. Similarly, in a stack, you can only remove or access elements from the top, not from the middle or bottom. This makes stacks useful in situations like undo operations in software, expression evaluation, and function call management in programming.
Example of Stack

The above image represent a stack data structure where elements are stored one above the other. 23 is the topmost element of the stack.
Why Stack is Linear Data Structure ?
A stack is considered a linear data structure because its elements are arranged in a sequential order, one after another. Even though we can only add or remove elements from the top, each element in the stack are arranged in a order. This sequential arrangement allows us to traverse the elements one by one from top to bottom if needed. Unlike non-linear structures like graphs or trees, where elements can have multiple connections or branches, a stack maintains a single, ordered sequence of elements, which is why it is classified as linear data structure.
LIFO Principle
The LIFO principle stands for Last-In-First-Out. It is the rule that a stack follows, which means the last item added to the stack will be the first one to be removed.
Simple Example of LIFO:
Imagine a stack of books on a table:
- You place Book A on the table.
- Then you put Book B on top of Book A.
- Finally, you put Book C on top.
When you start taking books off the stack, you take Book C first (last added), then Book B, and finally Book A (first added).
This shows the LIFO principle: Last-In, First-Out.
How is a stack implemented ?
Stack is implemented using
- Array
- Linked List
Why Operations are needed in a stack ?
Operations are needed in a stack because a stack is a Linear data structure that follows the LIFO (Last In, First Out) principle, and you cannot just access elements randomly like in an array. To use a stack properly, you need specific operations that allow you to add, remove, and inspect elements in a controlled way.
Stack Operations in Data Structure
There are below operations which we can perform on Stack
1. Push
2. Pop
3. Peek
1. Push Operation
Using push operation Individual items can be added to a stack. We can perform Push operation only at the top of the stack. However, before inserting an item in the stack we must first check if TOP=MAX–1 because if that is the case, then the stack is full and no more insertions can be done. If an attempt is made to insert a value in a stack that is already full, an OVERFLOW message is printed.
Lets see below example to understand push operation in data structure
We are going to perform PUSH operation on this stack

Now we want to insert an element with value 13, we first check if TOP=MAX–1. If the condition is false means there is some space in the stack, then we increment the value of TOP and store the new element at the position given by the stack[TOP]. After performing the PUSH operation our stack will look like this.

Code Snippet of PUSH Operation
public void push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
}
else {
a[++top] = x;
System.out.println("Item pushed into stack = "+x);
}
}
2. Pop Operation
Using pop operation Individual items can retrieved or removes from the stack. We can perform Pop operation only at the top of the stack. However, before deleting the value, we must first check if TOP=NULL because if that is the case, then it means the stack is empty and no more deletions can be done. If an attempt is made to delete a value from a stack that is already empty, an UNDERFLOW message is printed.
Lets see below example to understand pop operation in data structure
We are going to perform POP operation on this stack

To perform pop operation first check if TOP=NULL. If the condition is false means there are some elements in the stack, then we decrement the value pointed by TOP. After performing POP operation our stack will look like.

Code Snippet of POP Operation
public int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
3. Peek Operation
Using peek operation topmost element of the stack will be retrieved without deleting it from the stack. However, the Peek operation first checks if the stack is empty, i.e., if TOP = NULL, then an appropriate message is printed, else the value is returned.
Code Snippet of PEEK Operation
public int peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
When an item is added to a stack, it is placed on the top of all previously entered items. When an item is removed, it can be removed from the top of the stack. A stack in which items are removed the top is considered a “LIFO” (Last In, First Out) stack.
Time & Space Complexity
| Operation | Array Implementation | Linked List Implementation |
|---|---|---|
| Push | O(1) | O(1) |
| Pop | O(1) | O(1) |
| Peek/Top | O(1) | O(1) |
| Implementation Type | Space Complexity |
|---|---|
| Array-based Stack | O(n) |
| Linked List Stack | O(n) |
Stack Applications in Data Structure
1). Stack is used to tracking function calls for example Suppose we have two functions function A and function B. function A calls another function B and function B calls function C.
2). “undo” operation in text editors is an application of stack
3). Solving Infix, prefix, and postfix expression
4). Forward and backward feature in web browsers
5). Backtracking is also an application of Stack.
6). In Memory management.
Conclusion
A stack is an efficient data structure that works on the LIFO principle. Its main operations — push, pop, and peek — all take O(1) time, making it very fast. The space complexity for both array-based and linked list-based stacks is O(n), depending on the number of elements stored. This makes stacks simple, powerful, and widely used in algorithms, memory management, and expression evaluation.