• 沒有找到結果。

Stack Hsuan-TienLin March31,2020

N/A
N/A
Protected

Academic year: 2022

Share "Stack Hsuan-TienLin March31,2020"

Copied!
11
0
0

加載中.... (立即查看全文)

全文

(1)

Stack

Hsuan-Tien Lin

Dept. of CSIE, NTU

March 31, 2020

(2)
(3)

Stack

mimic: “pile of documents” on your desk

(4)

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

push 3

push 5

pop -> 5

(5)

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?

(6)

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

(7)

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?

(8)
(9)

Stacks Implemented on Array

usually: (growable) consecutive array and push/pop at end-of-array

(10)

Stacks Implemented on Linked List

usually: singly linked list and push/pop at head

H.-T. Lin (NTU CSIE) Stack 9/19

(11)

Stack in STL

1 stack < i n t , vector <i n t> > s_on_array ;

2 stack < i n t , l i s t <i n t> > s_on_array ; implemented as containeradapter

參考文獻

相關文件

Note: Automatic variables are allocated within the stack frames.

In this paper, we address the high cost of collecting such training data by presenting a weakly supervised approach to object saliency detection, where only image-level

Continuation of Suspension of Face-to-face Classes for Schools in Hong Kong Since the latest epidemic situation is still very severe, the Education Bureau (EDB)

To have a good mathematical theory of D-string world-sheet instantons, we want our moduli stack of stable morphisms from Azumaya nodal curves with a fundamental module to Y to

This kind of algorithm has also been a powerful tool for solving many other optimization problems, including symmetric cone complementarity problems [15, 16, 20–22], symmetric

In this paper, we follow the ideas used in the aforementioned two effective methods to solve MCP (1) whose solution may not be unique. For each method, we design a similar algorithm

• 如果把 queue 換成

Return address Local variables Previous frame pointer?. Return address