Stacks, Queues, Deques
Hsuan-Tien Lin
Dept. of CSIE, NTU
March 31, 2015
H.-T. Lin (NTU CSIE) Stacks, Queues, Deques 03/31/2015 0 / 3
Stacks
Stack
object: a container that holds some elements
action: [constant-time] push (to the top), pop (from the top)
last-in-first-out (LIFO): 擠電梯 , 洗盤子
very restricted data structure, but important for computers
—will discuss some cases later
H.-T. Lin (NTU CSIE) Stacks, Queues, Deques 03/31/2015 1 / 3
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?
H.-T. Lin (NTU CSIE) Stacks, Queues, Deques 03/31/2015 2 / 3
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
H.-T. Lin (NTU CSIE) Stacks, Queues, Deques 03/31/2015 3 / 3