VOOZH about

URL: https://www.geeksforgeeks.org/dsa/basic-operations-in-stack-data-structure-with-implementations/

⇱ Basic Operations in Stack Data Structure - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Basic Operations in Stack Data Structure

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:

  • push() to insert an element into the stack
  • top() Returns the top element of the stack.
  • pop() to remove an element from the stack
  • isEmpty() returns true if the stack is empty else false.
  • size() returns the size of the stack.

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)

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)

Output
3 

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)

Output
3 2 1 

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)

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. 


Output
0
2

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

Comment
Article Tags:
Article Tags: