0
Explore
0

Stack in Data Structure with its Application and Example

Updated on October 4, 2025

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

what is stack in data structure

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 ?

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:

  1. You place Book A on the table.
  2. Then you put Book B on top of Book A.
  3. 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 principleLast-In, First-Out.

How is a stack implemented ?

Stack is implemented using

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

stack push operation

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

pop operation in data structure

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

Time Complexity

OperationArray ImplementationLinked List Implementation
PushO(1)O(1)
PopO(1)O(1)
Peek/TopO(1)O(1)

Space Complexity

Implementation TypeSpace Complexity
Array-based StackO(n)
Linked List StackO(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.