Sunday, January 31, 2021

Stacks Data Structcure

 Stack is a linear data structure. It follows a special order in which operations are performed. The order is LIFO or FILO. 

This can be understood by an easy example. A stack of plate. As you can imagine, the plate on the top is removed first which means that the last plate added is moved first which is LIFO.

In the same way, the First In plate will be removed at the last which is FILO. So we can conclude that the first plate added will remain for the longest time.


3 operations: Push- to add an item in the stack. If the stack is full, it is said to be an underflow condition.

Pop- to remove an item from the stack. The items are popped out in the reverse order in which they are pushed.

Peek or Top- to return the top element of stack.

isEmpty- returns true if stack is empty else false.


Time complexity: pop() push() isEmpty() peek() all takes O(1). We do not use any loop in these operations.

Application: 1. Redo Undo features for apps like editors, photoshop.

2. Forward and backword feature in web browsers.

Implementation: We can implement a stack using array and linked list.





0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home