Stack
Hsuan-Tien Lin
Dept. of CSIE, NTU
March 31, 2020
Intuition
Intuition
Stack
Intuition
Stack: Last-In-First-Out (LIFO) Stack
(constant-time) operations:
• insertTop(data), often called push(data)
• removeTop(), often called pop()
• getTop(), often called peek()
—LIFO: 擠電梯 , 洗盤子
very restricted data structure, but important for computers
—will discuss some cases later
H.-T. Lin (NTU CSIE) Stack 3/19
Intuition
A Simple Application: Parentheses Balancing
• in C, the following characters show up in pairs: (), [], {}, ""
good: {xxx(xxxxxx)xxxxx"xxxx"x}
bad: {xxx(xxxxxx}xxxxx"xxxx"x}
• the LISP programming language
(append (pow (* (+ 3 5) 2) 4) 3) how can we check parentheses balancing?
Intuition
Stack Solution to Parentheses Balancing
inner-most parentheses pair =⇒ top-most plate
’(’: 堆盤子上去 ; ’)’: 拿盤子下來 Parentheses Balancing Algorithm
for each c in the input do if c is a left character
push c to the stack else if c is a right character
pop d from the stack and check if match end if
end for
many more sophisticated use in compiler design (will see some)
H.-T. Lin (NTU CSIE) Stack 5/19
Intuition
System Stack
• recall: function call ⇔ 拿新的草稿紙來算
• old (original) scrap paper: temporarily not used, 可以壓在下面 System Stack: 一疊草稿紙 , each paper (stack frame) contains
• return address: where to return to the previous scrap paper
• local variables (including parameters): to be used for calculating within this function
• previous frame pointer: to be used when escaping from this function
some related issues: security attack?
Implementation
Implementation
Stacks Implemented on Array
usually: (growable) consecutive array and push/pop at end-of-array
Implementation
Stacks Implemented on Linked List
usually: singly linked list and push/pop at head
H.-T. Lin (NTU CSIE) Stack 9/19
Implementation
Stack in STL
1 s t a c k < i n t , v e c t o r <i n t> > s_on_array ;
2 s t a c k < i n t , l i s t <i n t> > s_on_array ; implemented as containeradapter
Application: Expression Evaluation
Application: Expression Evaluation
Stack for Expression Evaluation
a/b − c + d ∗ e − a ∗ c
• precedence: {∗, /} first; {+, −} later
• steps
• f = a/b
• g = f − c
• h = d ∗ e
• i = g + h
• j = a ∗ c
• ` =i − j
Postfix Notation
same operand order, but put “operator”afterneeded operands
—can “operate” immediately when seeing operator
—no need to look beyond for precedence
Application: Expression Evaluation
Postfix from Infix (Usual) Notation
• infix:
3 / 4 − 5 + 6 ∗ 7 − 8 ∗ 9
• parenthesize:
3 / 4 − 5 + 6 ∗ 7 − 8 ∗ 9
• for every triple in parentheses, switch orders
• remove parentheses
difficult to parenthesize efficiently
H.-T. Lin (NTU CSIE) Stack 13/19
Application: Expression Evaluation
Evaluate Postfix Expressions
34/5 − 67 ∗ +89 ∗ −
• how to evaluate? left-to-right, “operate” when see operator
• 3, 4, / ⇒ 0.75
• 0.75, 5, - ⇒ -4.25
• -4.25, 6, 7, * ⇒ -4.25, 42 (note: -4.25 stored for latter use)
• -4.25, 42, + ⇒ 37.75
• 37.75, 8, 9, * ⇒ 37.75, 72 (note: 37.75 stored for latter use)
• 37.75, 72, - ⇒ ...
stored where?
stackso closest operands will be considered first!
Application: Expression Evaluation
Stack Solution to Postfix Evaluation
Postfix Evaluation
for each token in the input do if token is a number
push token to the stack else if token is an operator
sequentially pop operands at−1, · · · ,a0from the stack push token(a0,a1,at−1)to the stack
end if end for
return the top of stack
matches closely with the definition of postfix notation
H.-T. Lin (NTU CSIE) Stack 15/19
Application: Expression Parsing
Application: Expression Parsing
One-Pass Algorithm for Infix to Postfix
infix ⇒ postfix efficiently?
• at/, not sure of what to do (need later operands) sostore a/b − c + d ∗ e − a ∗ c
• at-, know that a / b can be a b / because-is of lower precedence a/b−c + d ∗ e − a ∗ c
• at+, know that ? - c can be ? c - because+is of same precedence but {-,+} is left-associative
a/b − c+d ∗ e − a ∗ c
• at*, not sure of what to do (need later operands) sostore a/b − c + d∗e − a ∗ c
stored where?stackso closest operators will be considered first!
H.-T. Lin (NTU CSIE) Stack 17/19
Application: Expression Parsing
Stack Solution to Infix-Postfix Translation
for each token in the input do if token is a number
output token
else if token is an operator
while top of stack is of higher (or same) precedence do pop and output top of stack
end while
push token to the stack end if
end for
• here: infix to postfix with operator stack
—closest operators will be considered first
• recall: postfix evaluation with operand stack
—closest operands will be considered first
Application: Expression Parsing
Some More Hints on Infix-Postfix Translation
for each token in the input do if token is a number
output token
else if token is an operator
while top of stack is of higher (or same) precedence do pop and output top of stack
end while
push token to the stack end if
end for
• for left associativity and binary operators
• right associativity? same precedence needs to wait
• unary/trinary operator? same
• parentheses? higest priority
• at ’(’, cannot pop anything from stack
—like seeing ’*’ while having ’+’ on the stack
• at ’)’, can pop until ’(’ —like parentheses matching
H.-T. Lin (NTU CSIE) Stack 19/19