Basic Operations in Stack Data Structure (original) (raw)

Last Updated : 22 Sep, 2025

Stack is a linear data structure that follows the LIFO (Last In First Out) principle for inserting and deleting elements from it.

In order to work with a stack, we have some fundamental operations that allow us to insert, remove, and access elements efficiently. These include:

We will now see how to perform these operations on Stack.

Push Operation in Stack:

Push operation is used to insert an element onto the top of the stack.

Push-Operation-in-Stack-(1)

C++ `

#include using namespace std;

int main() {

// creating a stack of integers
stack<int> st;  

// This pushes 1 to the stack top
st.push(1); 

// This pushes 2 to the stack top
st.push(2); 

// This pushes 3 to the stack top
st.push(3);  

}

Java

import java.util.Stack;

class GfG { public static void main(String[] args) {

    // creating a stack of integers
    Stack<Integer> st = new Stack<>();

    // pushing elements into the stack
    
    // pushes 1
    st.push(1);  
    
    // pushes 2
    st.push(2);  
    
    // pushes 3
    st.push(3);  
}

}

Python

if name == "main": # creating a stack of integers st = []

# pushing elements into the stack

# pushes 1
st.append(1) 

# pushes 2
st.append(2)

# pushes 3
st.append(3)  

C#

using System; using System.Collections.Generic;

class GfG { static void Main() {

    // creating a stack of integers
    Stack<int> st = new Stack<int>();

    // pushing elements into the stack
    st.Push(1);  // pushes 1
    st.Push(2);  // pushes 2
    st.Push(3);  // pushes 3
}

}

JavaScript

// Driver Code // creating a stack using array let st = [];

// pushing elements into the stack st.push(1); // pushes 1 st.push(2); // pushes 2 st.push(3); // pushes 3

`

**Time Complexity: O(1), since insertion at the top takes constant time.
**Auxiliary Space: O(1)

**Note: If the stack is implemented using a fixed-size array, inserting an element into a full stack will cause an overflow condition.

Top or Peek Operation in Stack:

Top or Peek operation is used to get the top element of the stack.

Top-or-Peek-Operation-in-Stack-(1)

C++ `

#include #include using namespace std;

int main() {

// creating a stack of integers
stack<int> st; 

st.push(1); 
st.push(2); 
st.push(3); 
    
// Printing Current top element 
cout << st.top() << " ";

return 0;

}

Java

import java.util.Stack;

class GfG { public static void main(String[] args) {

    // creating a stack of integers
    Stack<Integer> st = new Stack<>();
    
    st.push(1);
    st.push(2);
    st.push(3);

    // Printing current top element
    System.out.print(st.peek() + " ");
}

}

Python

if name == "main":

# creating a stack of integers
st = []

st.append(1)
st.append(2)
st.append(3)

# Printing current top element
print(st[-1])

C#

using System; using System.Collections.Generic;

class GfG { static void Main() {

    // creating a stack of integers
    Stack<int> st = new Stack<int>();
    
    st.Push(1);
    st.Push(2);
    st.Push(3);
    
    // Printing current top element
    Console.Write(st.Peek() + " ");
}

}

JavaScript

// Driver Code

// creating a stack using array let st = [];

st.push(1); st.push(2); st.push(3);

// Printing current top element console.log(st[st.length - 1].toString() + " ");

`

**Time Complexity: O(1)
**Auxiliary Space: O(1)

Pop Operation in Stack:

Pop operation is used to remove an element from the top of the stack.

The items are popped in the reversed order in which they are pushed.

Pop-Operation-in-Stack-(1)

C++ `

#include #include using namespace std;

int main() {

// creating a stack of integers
stack<int> st; 

st.push(1); 
st.push(2); 
st.push(3); 

// Printing Current top element 
cout << st.top() << " ";
    
// removes the top element from the stack
st.pop(); 

// Printing Current top element 
cout << st.top() << " ";
    
// removes the top element from the stack
st.pop(); 

}

Java

import java.util.Stack;

class GfG { public static void main(String[] args) {

    // creating a stack of integers
    Stack<Integer> st = new Stack<>();
    
    st.push(1);
    st.push(2);
    st.push(3);

    // Printing current top element
    System.out.print(st.peek() + " ");
        
    // removes the top element from the stack
    st.pop();
    
    // Printing current top element
    System.out.print(st.peek() + " ");
        
    // removes the top element from the stack
    st.pop();
}

}

Python

if name == "main":

# creating a stack of integers
st = []

st.append(1)
st.append(2)
st.append(3)

# Printing current top element
print(st[-1], end=" ")

# removes the top element from the stack
st.pop()

# Printing current top element
print(st[-1], end=" ")

# removes the top element from the stack
st.pop()

C#

using System; using System.Collections.Generic;

class GfG { static void Main() {

    // creating a stack of integers
    Stack<int> st = new Stack<int>();
    
    st.Push(1);
    st.Push(2);
    st.Push(3);
    
    // Printing current top element
    Console.Write(st.Peek() + " ");
        
    // removes the top element from the stack
    st.Pop();
            
    // Printing current top element
    Console.Write(st.Peek() + " ");
        
    // removes the top element from the stack
    st.Pop();
}

}

JavaScript

// Driver Code

// creating a stack using array let st = [];

st.push(1); st.push(2); st.push(3);

// Printing current top element process.stdout.write(st[st.length - 1] + ' ');

// removes the top element from the stack st.pop();

// Printing current top element process.stdout.write(st[st.length - 1] + ' ');

// removes the top element from the stack st.pop();

`

**Time Complexity: O(1)
**Auxiliary Space: O(1)

**Note: If a stack is empty, deleting an element will cause an underflow condition.

isEmpty Operation in Stack:

isEmpty operation is a boolean operation that is used to determine if the stack is empty or not. This will return true if the stack is empty, else false.

isEmpty-Operation-in-Stack-(1)

C++ `

#include #include using namespace std;

int main() {

stack<int> st; 

// if stack is empty returns true else false
if (st.empty()) { 
    cout << "Stack is empty." << endl; 
} 
else { 
    cout << "Stack is not empty." << endl; 
} 

// Inserting value 1 to the stack top 
st.push(1);

// if stack is empty returns true else false
if (st.empty()) { 
    cout << "Stack is empty." << endl; 
} 
else { 
    cout << "Stack is not empty." << endl; 
} 

}

Java

import java.util.Stack;

public class GfG { public static void main(String[] args) { Stack st = new Stack<>();

    // if stack is empty returns true else false
    if (st.isEmpty()) {
        System.out.println("Stack is empty.");
    } else {
        System.out.println("Stack is not empty.");
    }

    // Inserting value 1 to the stack top
    st.push(1);

    // if stack is empty returns true else false
    if (st.isEmpty()) {
        System.out.println("Stack is empty.");
    } else {
        System.out.println("Stack is not empty.");
    }
}

}

Python

if name == "main": st = []

# if stack is empty returns true else false
if len(st) == 0:
    print("Stack is empty.")
else:
    print("Stack is not empty.")

# Inserting value 1 to the stack top
st.append(1)

# if stack is empty returns true else false
if len(st) == 0:
    print("Stack is empty.")
else:
    print("Stack is not empty.")

C#

using System; using System.Collections.Generic;

class GfG { static void Main() {

    Stack<int> st = new Stack<int>();
    
    // if stack is empty returns true else false
    if (st.Count == 0) {
        Console.WriteLine("Stack is empty.");
    } else {
        Console.WriteLine("Stack is not empty.");
    }
    
    // Inserting value 1 to the stack top
    st.Push(1);
    
    // if stack is empty returns true else false
    if (st.Count == 0) {
        Console.WriteLine("Stack is empty.");
    } else {
        Console.WriteLine("Stack is not empty.");
    }
}

}

JavaScript

// Driver Code let st = [];

// if stack is empty returns true else false if (st.length === 0) { console.log("Stack is empty."); } else { console.log("Stack is not empty."); }

// Inserting value 1 to the stack top st.push(1);

// if stack is empty returns true else false if (st.length === 0) { console.log("Stack is empty."); } else { console.log("Stack is not empty."); }

`

Output

Stack is empty. Stack is not empty.

**Time Complexity: O(1)
**Auxiliary Space: O(1)

Size Operation in Stack:

Size operation in Stack is used to return the **count of elements that are present inside the stack.

C++ `

#include #include using namespace std;

int main() {

stack<int> st; 

// Checking Current stack size
cout << st.size() << endl;

st.push(1);
st.push(2); 

// Checking current stack size 
cout << st.size() << endl; 

}

Java

import java.util.Stack;

public class Main { public static void main(String[] args) { Stack st = new Stack<>();

    // Checking Current stack size
    System.out.println(st.size()); 
    
    st.push(1); 
    st.push(2); 
    
    // Checking Current stack size
    System.out.println(st.size());
}

}

Python

if name == "main": st = []

# Checking current stack size
print(len(st))

st.append(1)
st.append(2)

# Checking current stack size
print(len(st))

C#

using System; using System.Collections.Generic;

class GfG { static void Main() { Stack st = new Stack();

    // Checking current stack size
    Console.WriteLine(st.Count);

    st.Push(1);
    st.Push(2);

    // Checking current stack size
    Console.WriteLine(st.Count);
}

}

JavaScript

// Driver Code let st = [];

// Checking current stack size console.log(st.length);

st.push(1); st.push(2);

// Checking current stack size console.log(st.length);

`

**Time Complexity: O(1)
**Auxiliary Space: O(1)