Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
- PUSH - Adds an item in the stack and increments the
top
pointer. If the stack is full, then it is said to be an Overflow condition. - POP - Removes an item from the stack and decrements the
top
. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. - PEEK - Returns top element of stack. Does not remove any elements from stack.
- isEmpty - Returns true if stack is empty, else false.
- DISPLAY - Displays content of stack.
- getSize - Returns the size of the stack irrespective of how many elements are in the stack.
Stack can be implemented using arrays, linked list or queues. Implementation using arrays is the simplest one.
- push(), pop(), isEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.
- Expression evaluation
- Expression conversion
- Syntax parsing
- String reversal
- Implementation of Tower of Hanoi Problem
- Function call and Recursion
- Parenthesis checking
- Backtracking
- File undo and redo operations
- Depth First Search traversal in tree and graph data structure uses stack to store explored nodes and processes them later.