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 |
2. 2. A Stack Implementation
In practice , “use a Python list as a stack”
even though the implementations are logically equivalent, they would have very different timings when performing benchmark testing.
Here, just use python list with limit function.
3. 3. Example:Balanced Parentheses
parentheses are used to order the performance of operations
$$
(5+6)×(7+8)/(4+3)
$$
or lisp function
1 | (defun square(n) |
So,it’s important to differentiate between parentheses that are correctly balanced and those that are unbalanced in strucures.
- Why Stack ? open match close\ close match open, reverse order
- So a stack, push if open, pop if close. Finally, if len(stack)==0, balance.
1 | PAIRINGS = { |
4. 4. Example: Converting Number Bases
$$
233_{10}=11101001_{2}
$$
- Why stack?
Divide by Base
algorithm
- So, Push fisrt, Pop at the end
1 | DIGITS = '0123456789abcdef' |
5. 5. Example: Infix, Prefix and Postfix Expressions
Infix expression | Prefix expression | Postfix expression |
---|---|---|
A + B | + A B | A B + |
A + B * C | + A * B C | A B C * + |
- Infix expression need parenthese to force the performance of addition before multiplication.
- Prefix & Postfixexpression DON’T need it
How to convert infix to prefix or postfix?
Fully parenthesize the expression using the order of operations. Then move the enclosed operator to the position of either the left or the right parenthesis depending on whether you want prefix or postfix notation.
5.1. 5.1 Algorithm
- Create an empty stack called
operation_stack
for keeping operators. Create an empty list for output. - Convert the input infix string to a list by using the string method
split
. - Scan the token list from left to right.
- If the token is an operand, append it to the end of the output list.
- If the token is a left parenthesis, push it on the
operation_stack
. - If the token is a right parenthesis, pop the
operation_stack
until the corresponding left parenthesis is removed. Append each operator to the end of the output list. - If the token is an operator,
*
,/
,+
, or-
, push it on theoperation_stack
. However, first remove any operators already on theoperation_stack
that have higher or equal precedence and append them to the output list.
- When the input expression has been completely processed, check the
operation_stack
. Any operators still on the stack can be removed and appended to the end of the output list.
5.2. 5.2 Implementation
1 | PRECEDENCE = { |