![]() |
VOOZH | about |
Stack is a linear data structure that follows a particular order in which the elements are inserted and deleted. A stack follows the principle of last in first out(LIFO) which means that the last element inserted into the stack should be removed first. Consider an example of plates stacked over one another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order. A stack can be implemented using Arrays or Linked Lists.
This operation pushes an element on top of the stack and the top pointer points to the newly pushed element. It takes one parameter and pushes it onto the stack.
Below is the implementation of push() using Array :
1 pushed into stack successfully ! 2 pushed into stack successfully !
Complexity Analysis:
Below is the implementation of push() using Linked List :
1 pushed into stack successfully ! 2 pushed into stack successfully !
Complexity Analysis:
This operation removes the topmost element in the stack and returns an error if the stack is already empty.
Below is the implementation of pop() using Array:
1 pushed into stack successfully ! Element popped from stack : 1 Stack Underflow
Complexity Analysis:
Below is the implementation of pop() using Linked List :
1 pushed into stack successfully ! Element popped from stack : 1 Stack Underflow
Complexity Analysis:
This operation prints the topmost element of the stack.
Below is the Implementation of peek() using Array:
1 pushed into stack successfully ! 2 pushed into stack successfully ! Peek element of stack : 2
Complexity Analysis:
Below is the implementation of peek() using Linked List :
1 pushed into stack successfully ! Peek element of stack : 1
Complexity Analysis:
This operation tells us whether the stack is empty or not.
Below is the implementation of isempty() using Array :
1
Complexity Analysis:
Below is the implementation of isempty() using Linked List :
1
Complexity Analysis:
This operation returns the current size of the stack.
Below is the implementation of size() using Array:
1 pushed into stack successfully ! 2 pushed into stack successfully ! The size of the stack is 2
Complexity Analysis:
Below is the implementation of size() using Linked List:
Size of stack : 3
Complexity Analysis: