1 Chapter 8 Stacks and Queues. 2 This is a stack of books.

Slides:



Advertisements
Similar presentations
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Advertisements

布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
Introduction to Java Programming Lecture 13 Classes I OO Programming.
建立使用案例敘述 --Use Case Narrative
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
如何將數字變成可用之資訊 現代化資料處理與應用概念. 如何將數字變成可用之資訊 人最容易接受的訊息是圖像化資訊。 在一堆數字中,要進行比較分析,一般會使用表格形 式計算與分析。 所以一般我們會將數字依關聯性, 轉換成表格計算與分析。 此表格一般稱試算表或稱表格。 再將結果轉換為圖表,進行比較與分析。
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
指導教授:陳淑媛 學生:李宗叡 李卿輔.  利用下列三種方法 (Edge Detection 、 Local Binary Pattern 、 Structured Local Edge Pattern) 來判斷是否為場景變換,以方便使用者來 找出所要的片段。
Graph V(G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E(G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5,
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
Concept Map – whatwhat, why and how?whyhow. What is Concept Mapping ?  Concept mapping is a technique for representing knowledge in graphs. Knowledge.
Chapter 07 低階程式語言.
國立中山大學財產管理系統 線上報廢、盤點系統 總務處保管組 策劃 計算機與網路中心 分析設計 2008/03.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Introduction to Java Programming Lecture 15 Objects and Classes.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
:Problem D: Bit-wise Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10232: Problem D: Bit-wise Sequence 解題者:李濟宇 解題日期: 2006 年 4 月 16.
24-6 設定開始與結束場景中的 程式 最後我們要替這個遊戲收個尾, 幫它把開始 的等待畫面跟結束畫面處理一下。
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
最新計算機概論 第 5 章 系統程式. 5-1 系統程式的類型 作業系統 (OS) : 介於電腦硬體與 應用軟體之間的 程式,除了提供 執行應用軟體的 環境,還負責分 配系統資源。
Chapter 20 塑模動態觀點:狀態圖 Statechart Diagram. 學習目標  說明狀態圖的目的  定義狀態圖的基本記號  展示狀態圖的建構  定義活動、內部事件及遞延事件的狀態 圖記號.
ENQUEUE “MATT” MATT FRONTBACK ENQUEUE “ANDREW”
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
第十章 再論結構. 指標與結構的關係 /* File name: ex10-1a.c */ #include int main() { struct student { char *name; int score; }; struct student st= {"Brian", 97}; struct.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
: Count DePrimes ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11408: Count DePrimes 解題者:李育賢 解題日期: 2008 年 9 月 2 日 題意: 題目會給你二個數字 a,b( 2 ≦ a ≦ 5,000,000,a.
Fugacity Coefficient and Fugacity
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
4 堆疊與佇列 4.1 前言 四種基本的資料結構 (可儲存資料的容器) 陣列 (Array)、串列(List): 最基本
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
: GCD - Extreme II ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11426: GCD - Extreme II 解題者:蔡宗翰 解題日期: 2008 年 9 月 19 日 題意: 最多 20,000 組測資,題目會給一個數字.
Image Interpolation Use SSE 指導教授 : 楊士萱 學 生 : 楊宗峰 日 期 :
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 10 m-way 搜尋樹與B-Tree
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
計算機概論 第6章 數位邏輯設計.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
中序轉後序 藉由由左向右掃瞄中序運算式產生後序運算式,遇到 運算元就直接輸出,遇到運算符號則先存入堆疊,將 優先權較高者輸出。 範例: a + b * c TokenStack [0] [1] [2] topoutput aa ++0a b+0ab *+ *1ab c+ *1abc eosabc*+
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 壹 企業研究導論.
Visual C++重點複習.
資料結構實習-六.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
: SAM I AM ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11419: SAM I AM 解題者:李重儀 解題日期: 2008 年 9 月 11 日 題意: 簡單的說,就是一個長方形的廟裡面有敵人,然 後可以橫的方向開砲或縱向開砲,每次開砲可以.
Introduction to Java Programming Lecture 16 Super and Sub Classes.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
:Rings and Glue ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10301: Rings and Glue 解題者:施博修 解題日期: 2011 年 5 月 18 日 題意:小約翰有了個大麻煩,他不小心將 rings.
Concept Map – whatwhat, why and how?whyhow. Outline.
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
PUSH “MATT” MATT FRONTBACK PUSH “ANDREW” MATT FRONT ANDREW BACK.
節能轉接插座 認知科學研究所陳啟彰. 設計緣起 不使用的電器如未將插頭拔除, 仍會有少量的電力損耗,這類的 電力損耗稱之為待機損耗 (stand- by loss) 。 不使用的電器如未將插頭拔除, 仍會有少量的電力損耗,這類的 電力損耗稱之為待機損耗 (stand- by loss) 。 家庭用電中,待機損耗約佔總耗.
1 Chap. 7 Response of First-Order RL and RC Circuits Contents 7.1 The Natural Response of an RL Circuit 7.2 The Natural Response of an RC Circuit 7.3 The.
Presentation transcript:

1 Chapter 8 Stacks and Queues

2 This is a stack of books.

3 This is a queue.

4 Chap.8 Contents 8.1 Stacks The PureStack Interface Implementations of the PureStack Interface Stack Application 1: How Compilers Implement Recursion Stack Application 2: Converting from Infix to Postfix Prefix Notation 8.2 Queues The PureQueue Interface Implementations of the PureQueue Interface Computer Simulation Queue Application: A Simulated Car Wash

5 8.1 Stacks

6

7 Object-Oriented (O-O) Terms Stack, Queue 是特別的 list. 以 O-O 術語, 它們 EXTEND ( 延伸 ) list (ArrayList 或 LinkedList) General concepts ( 通用觀念 ) are Super class such as ArrayList Specialized concepts ( 專用觀念 ) are Sub class such as Stack

8

9

10

11

12

13

14

The PureStack Interface

16

17

實作 PureStack Interface

19

20

21

22

23

24 寫出下列的輸出結果 : LinkedListPureStack myStack = new LinkedListPureStack ( ); for (int i = 0; i < 10; i++) myStack.push (i * i); while (!myStack.isEmpty( )) System.out.println (myStack.pop( )); ANS: 81 (9*9), 64 (8*8), 49, 36, 25, 16, 9, 4, 1, 0

Stack 的應用 1: Compiler 如何實作 Recursive

26

27 Run-time stack 處理 activation records. Push: When method is called Pop: When execution of method is completed

28

29

30

31

32

Stack 應用 2: 轉換 Infix( 中置式 ) 到 Postfix( 後置式 ) 在 infix 表示法中, operator( 運算子 ) 放置於 operands( 運算元 ) 之間. For example: a + b c – d + (e * f – g * h) / i

34 Old compilers: Infix Machine language 這會因為 parentheses( 小括號 ) 而產生混亂. Newer compilers: Infix Postfix Machine language

35 在 postfix 表示法中, an operator 直接地放置 於他的 operands 之後. No parenthesis needed. InfixPostfix a + bab+ a + b * cabc*+ a * b + cab*c+ (a + b) * cab+c*

36 postfix 不必使用 Parentheses 很棒 !

37 Let’s convert an infix string below to a postfix string. x – y * z ANS: xyz*-

38 Postfix 保留 operands 的先後順序, so an operand can be appended to postfix as soon as that operand is encountered in infix.

39 InfixPostfix x – y * z x

40 InfixPostfix x – y * z x The operands for ‘-’ 尚未在 postfix, 所以 ‘ - ’ 一定要先暫存在某地方.

41 InfixPostfix x – y * z xy

42 InfixPostfix x – y * z xy The operands for ‘*’ 未在 postfix, 所以 ‘*’ 一定要暫存在某地方, 且儲存在 ‘-’ 之前.

43 InfixPostfix x – y * z xyz

44 InfixPostfix x – y * z xyz* – ANS.

45 As another test case, we start with x*y-z. After moving ‘x’ to postfix, ‘*’ is temporarily saved, and then ‘y’ 被加到 postfix. What happens when ‘-’ is accessed? InfixPostfix x * y – z xy

46

47 暫時儲存處是 : stack ! Here is the strategy (pseudo-code) for maintaining the stack:

48

49 口訣 : if Infix Greater, then Push

50 Convert from infix to postfix: InfixPostfix a + b * c / d - e

51 Infix Postfix a + b * c / d – e abc*d/+e – * + Operator stack

52

53 Convert to postfix: x * (y + z)

54

55

56

57

58 Infix token

59 Tokens

60

61 12

Prefix( 前置式 ) Notation

63

64

65

66

67

68

Queues

70

71 回想一下; STACK: (Last-In-First-Out) LIFO

72 Enqueue “ 張三 ”

73 張三 FrontBack

74 Enqueue “ 李四 ”

75 張三 Front 李四 Back

76 Enqueue “ 王五 ”

77 王五張三 Front 李四 Back

78 Dequeue

79 王五 Front 李四 Back

The PureQueue Interface

81

82

83

Implementations of the PureQueue Interface

85

86

87 Heavy Inheritance Tax 在 class reuse 實務上, 被 reuse 的 class 要有完整 method description developers 清楚了解後, 再寫 dummy code ( 如上述 get) 來 override 不能 reuse 的 methods. 這是相當沉重的負擔,有如稅負,故叫 Inheritance Tax.

88

89

90 Determine the output from the following: LinkedListPureQueue myQueue = new LinkedListPureQueue (); for (int i = 0; i < 10; i++) myQueue.enqueue (i * i); while (!myQueue.isEmpty( )) System.out.println (myQueue.dequeue( )); ANS: 0(0*0),1(1*1),4, 9, 16, 25, 36, 49, 64, 81

Computer Simulation( 模擬 ) SYSTEM 是由互動的部分 (interacting parts) 所組成

92 model 是 system 的簡化. 建構 model 是為了研究 system

93

94

95

96

97

98

99

Queue Application: A Simulated Car Wash

101 carWash Minimal waiting time is 0 min. Maximal waiting time:10 min*5car= 50 min car queue arrival departure from queue to queue Wash Station

102

103

104

105

106

107

108

109

110

111

112

113

114

115 Car class /* Car * 初始化 Car object * 為指定的下次到達時間 */ public Car (int nextArrivalTime) /* getArrivalTime /* 記錄 the just dequeued car 的 arrival time. the arrival time of this car */ public int getArrivalTime()

116

117

118

119

120

121

122

123

124

125

126

127 public class Car{ protected int arrivalTime; public Car(){}//default constructor, //for the sake of sub-classses of Car public Car (int nextArrivalTime){ arrivalTime=nextArrivalTime; }// constructor with int parameter public int getArrivalTime(){ return arrivalTime; }/*getArrivalTime*/ }/*Car*/

128

129

130