×

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!

Stack Terminologies & Operations (Complete Tutorial in C++)



Last Updated on: 4th Jan 2026 16:55:38 PM

To understand how a stack works internally, it is important to learn its terminologies and basic operations. Stack terminologies help us understand the state of the stack, while stack operations define how elements are added, removed, and accessed.

 

Stack operations are simple, fast, and efficient because all actions are performed at a single end of the stack. This makes stack one of the most widely used data structures in programming.

 

Stack Terminologies

Stack terminologies are special terms used to describe different conditions and components of a stack. These terms help programmers manage stack operations safely and efficiently.

The most important stack terminologies are:

  • Top

  • Overflow

  • Underflow

 

Top

The Top is a variable that always stores the index or address of the last inserted element in the stack.

 

Explanation:

  • When the stack is empty,  top = -1  

  • When an element is pushed,  top  is increased

  • When an element is popped,  top  is decreased

 

Real-Life Example:

Think of a stack of books:

  • The book at the very top is always visible

  • You can only add or remove books from the top

 

The top pointer always tells which book is currently on top.

 

Overflow

Stack Overflow is a condition that occurs when we try to insert an element into a stack that is already full.

 

Explanation:

  • Happens in array-based stack

  • Occurs when   top == max_size - 1 

  • No more memory is available in stack

 

Real-Life Example:

Imagine a glass filled with water:

  • If you try to pour more water, it will overflow

 

Similarly, inserting into a full stack causes stack overflow.

 

Underflow

Stack Underflow is a condition that occurs when we try to remove an element from an empty stack.

 

Explanation:

  • Happens when  top == -1 

  • No element is available to remove

 

Real-Life Example:

Trying to remove a plate from an empty plate stand results in underflow.

 

Stack Operations

Stack operations are the actions performed on a stack to manage data. The main stack operations are:

  • Push

  • Pop

  • Peek

 

All these operations work only on the top of the stack.

 

Push Operation

The push operation is used to insert an element at the top of the stack.

 

Steps:

  1. Check stack overflow

  2. Increase  top 

  3. Insert element at  stack[top] 

 

Real-Life Example:

Placing a new plate on top of an existing stack of plates.

 

C++ Code: Push Operation

#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;
    }
}

 

Pop Operation

The pop operation removes the top element from the stack.

 

Steps:

  1. Check stack underflow

  2. Store top element

  3. Decrease top

 

Real-Life Example:

Removing the top plate from a stack of plates.

 

C++ Code: Pop Operation

void pop() {
    if (top == -1) {
        cout << "Stack Underflow" << endl;
    } else {
        cout << stack[top] << " popped from stack" << endl;
        top--;
    }
}

 

Peek Operation

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

 

Explanation:

  • Used to view the top element

  • Stack size remains unchanged

 

Real-Life Example:

Looking at the top book without removing it from the pile.

 

C++ Code: Peek Operation

void peek() {
    if (top == -1) {
        cout << "Stack is empty" << endl;
    } else {
        cout << "Top element is: " << stack[top] << endl;
    }
}

 

Complete C++ Program (Push, Pop, Peek)

#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
        stack[++top] = value;
}

void pop() {
    if (top == -1)
        cout << "Stack Underflow" << endl;
    else
        top--;
}

void peek() {
    if (top == -1)
        cout << "Stack is empty" << endl;
    else
        cout << "Top element: " << stack[top] << endl;
}

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

 

Sample output

When you run the given C++ program, the output will be:

Top element: 30
Top element: 20

 

Explanation (Brief)

  • After pushing  10 20  , and  30 , the top element of the stack is  30 .

  • The first  peek()  displays  30 .

  • After  pop() , the top element becomes  20 .

  • The second  peek()  displays  20 .

 

Time Complexity of Stack Operations

Time complexity tells how fast stack operations execute as the number of elements increases.

 

Push Operation

Time Complexity: O(1)
Reason: Element is inserted directly at the top without traversal.

 

Pop Operation

Time Complexity: O(1)
Reason: Only the top element is removed.

 

Peek Operation

Time Complexity: O(1)
Reason: Top element is accessed directly.

 

Summary Table

Operation Time Complexity
Push O(1)
Pop O(1)
Peek O(1)

 

Conclusion

Stack terminologies and operations form the core of stack implementation. Understanding concepts like top, overflow, and underflow helps prevent runtime errors, while push, pop, and peek operations allow efficient data handling. Since all major stack operations run in constant time, stack is an efficient and powerful data structure used in many real-world applications.

 

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