×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Introduction to Stack (Data Structure)



Last Updated on: 4th Jan 2026 15:50:59 PM

A Stack is one of the most important and widely used linear data structures in computer science. It is used in almost every area of software development, from simple programs to complex operating systems. Understanding stack is essential for learning advanced topics like recursion, expression evaluation, and memory management.

 

The stack data structure follows a specific rule that controls how data is stored and removed. Because of its simple working principle, stack is easy to understand and very powerful in real-world applications.

 

What is a Stack?

A Stack is a linear data structure that stores elements in a specific order. In a stack, insertion and deletion of elements are allowed only from one end, which is called the top of the stack.

 

Simple Definition:

A stack is a collection of elements where operations are performed at only one end.

 

Technical Definition:

A stack is a linear data structure that follows a restricted access pattern in which elements can be inserted and removed only from the top.

 

Only two main operations are allowed:

  • Push – inserting an element into the stack

  • Pop – removing an element from the stack

 

Access to elements in the middle or bottom of the stack is not allowed directly.

 

Real-Life Example:

A stack of plates in a cafeteria:

  • You add a plate on the top

  • You remove a plate from the top

 

You cannot remove a plate from the middle. This is exactly how a stack works.

 

Stack Principle (LIFO – Last In, First Out)

Stack works on the principle of LIFO, which stands for Last In, First Out.

This means the element that is inserted last will be the first one to be removed.

 

Explanation:

  • The element inserted last is removed first.

  • The element inserted first is removed last.

 

Real-Life Example:

Imagine placing books one on top of another.

  • You place Book A first

  • Then Book B

  • Then Book C

 

When you remove a book, you will remove Book C first, because it is on top.
This behavior exactly matches the LIFO principle.

 

Real-Life Examples of Stack

Stacks are used in many real-life situations.

 

Common Real-Life Examples:

  • Stack of books on a table

  • Undo and redo operations in software

  • Back and forward navigation in web browsers

  • Function call management in programs

  • Expression evaluation in calculators

 

Simple Example:

When you press the Back button in a browser, the last visited page is removed first, which follows the stack principle.

 

Stack Representation

A stack can be represented in two main ways:

 

1. Logical Representation

  • Elements are arranged one above another.

  • The top pointer always points to the last inserted element.

 

Example:

| 30 |  ← Top
| 20 |
| 10 |

 

2. Memory Representation

Internally, a stack can be implemented using:

  • Array

  • Linked List

 

In both cases, a variable called top is used to keep track of the current top element.

 

Basic Stack Operations

Operation Description
Push Insert element at top
Pop Remove element from top
Peek View top element
isEmpty Check if stack is empty
isFull Check if stack is full (array)

 

C++ Example: Stack Using Array (Basic Representation)

#include <iostream>
using namespace std;

#define MAX 5

int stack[MAX];
int top = -1;

void push(int value) {
    if (top == MAX - 1) {
        cout << "Stack Overflow" << endl;
    } else {
        top++;
        stack[top] = value;
        cout << value << " pushed into stack" << endl;
    }
}

void display() {
    if (top == -1) {
        cout << "Stack is empty" << endl;
    } else {
        cout << "Stack elements: ";
        for (int i = top; i >= 0; i--) {
            cout << stack[i] << " ";
        }
        cout << endl;
    }
}

int main() {
    push(10);
    push(20);
    push(30);
    display();
    return 0;
}

 

Output:

10 pushed into stack
20 pushed into stack
30 pushed into stack
Stack elements: 30 20 10

 

Explanation of Output:

The last inserted element 30 appears at the top, and elements are displayed from top to bottom, showing the LIFO behavior.

 

Time Complexity of Stack Operations

Time Complexity helps us understand how fast stack operations are performed as the number of elements increases. One of the biggest advantages of stack is that its core operations are very efficient.

 

Stack operations always work on the top element only, so they do not require traversal of the entire data structure.

 

Push Operation

The push operation inserts an element at the top of the stack.

  • Only one element is added

  • No shifting or traversal is required

  • The top pointer is simply increased by one

 

Time Complexity:
O(1) (Constant Time)

 

Reason:

The operation always takes the same amount of time, regardless of the number of elements in the stack.

 

Pop Operation

The pop operation removes the top element from the stack.

  • Only the top element is removed

  • No looping or searching is required

  • The top pointer is decreased by one

 

Time Complexity:
O(1) (Constant Time)

 

Reason:

The operation directly accesses the top element, so execution time does not depend on stack size.

 

Peek Operation

The peek operation returns the value of the top element without removing it.

  • No insertion or deletion

  • Only one memory access

 

Time Complexity:
O(1) (Constant Time)

 

Reason:

Peek always accesses a single element at a fixed position.

 

Display Operation

The display operation prints all elements of the stack.

  • Requires traversing all elements

  • Number of steps depends on stack size

 

Time Complexity:
O(n) (Linear Time)

 

Reason:

Each element is visited once to display it.

 

Summary Table: Time Complexity of Stack Operations

Operation Time Complexity Explanation
Push O(1) Insert at top only
Pop O(1) Remove from top only
Peek O(1) Access top element
Display O(n) Traverse all elements

 

Basic Applications Overview of Stack

Stacks are used in many important computer science applications.

 

Major Applications:

  • Function call and recursion handling

  • Expression evaluation (infix, postfix, prefix)

  • Syntax checking in compilers

  • Undo and redo operations

  • Backtracking problems

  • Memory management

 

Real-Life Example:

When a function is called in a program, its details are pushed onto the stack. After execution, the function is removed from the stack.

 

Conclusion

A stack is a simple yet powerful data structure that follows the Last In, First Out principle. It allows insertion and deletion of elements from only one end, making it highly efficient for specific applications. By understanding stack concepts, representation, and real-life usage, students build a strong foundation for learning advanced data structure topics like recursion and expression evaluation.

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo