- LIFO concept - Last In First Out
- opposite to arrays, only one item can be read or removed at a given time - the last item inserted
- its size is predefined: only limited number of items can be added (pushed)
- stack overflow occurs when a new item is being added but underlying buffer is full
- underlying data structure can be array, vector, linked list
Usage:
- to check whether parentheses ({ }), braces ([ ]) and brackets (( )) are balanced
- to parse arithmetic expressions
- in algorithms applied for other data structures (e.g. traversing binary trees)
- Complexity: O(1)
- Algorithm: return a value of the item from the top of the stack (item that has been added last)
- Complexity: O(1)
- Algorithm: remove the item from the top of the stack (item that has been added last)
- Complexity: O(1)
- opposite to arrays, only one item can be read or removed at a given time - the last item inserted
- its size is predefined: only limited number of items can be added (pushed)
- stack overflow occurs when a new item is being added but underlying buffer is full
- underlying data structure can be array, vector, linked list
Usage:
- to check whether parentheses ({ }), braces ([ ]) and brackets (( )) are balanced
- to parse arithmetic expressions
- in algorithms applied for other data structures (e.g. traversing binary trees)
Operations:
Insertion (push):
- Algorithm: item is added on the top of the stack- Complexity: O(1)
Peek:
- Algorithm: return a value of the item from the top of the stack (item that has been added last)
- Complexity: O(1)
Removal:
- Algorithm: remove the item from the top of the stack (item that has been added last)
- Complexity: O(1)
No comments:
Post a Comment