![]() |
VOOZH | about |
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 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 is used to get the top element of the stack.
👁 Top-or-Peek-Operation-in-Stack-(1)3
Time Complexity: O(1)
Auxiliary Space: O(1)
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)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 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)Stack is empty. Stack is not empty.
Time Complexity: O(1)
Auxiliary Space: O(1)
Size operation in Stack is used to return the count of elements that are present inside the stack.
0 2
Time Complexity: O(1)
Auxiliary Space: O(1)