1. 1. Introduction to Stacks
Stacks is a kind of linear data structures.
What distinguishes one linear structure from another is where additions and removals may occur
1.1. 1.1 Stacks
A stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end.
Example: stack of books\plates, URL back button
Usage: reverse the order
1.2. 1.2 ADT
ADT | data represents |
---|---|
DS | physical implementation of an ADT |
Stack operation | Stack contents | Return value |
---|---|---|
s.is_empty() | [] | True |
s.push(4) | [4] | |
s.push(‘dog’) | [4, ‘dog’] | |
s.peek() | [4, ‘dog’] | ‘dog’ |
s.push(True) | [4, ‘dog’, True] | |
s.size() | [4, ‘dog’, True] | 3 |
s.is_empty() | [4, ‘dog’, True] | False |
s.push(8.4) | [4, ‘dog’, True, 8.4] | |
s.pop() | [4, ‘dog’, True] | 8.4 |
s.pop() | [4, ‘dog’] | True |
s.size() | [4, ‘dog’] | 2 |