Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
師大資工系ACM-ICPC讀書會:參、堆疊與佇列
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Maplewing
October 25, 2013
Programming
270
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
師大資工系ACM-ICPC讀書會:參、堆疊與佇列
Maplewing
October 25, 2013
More Decks by Maplewing
See All by Maplewing
師大資工系ACM-ICPC讀書會:肆、鏈結串列與二元樹
sinmaplewing
0
170
資訊實務應用讀書會 第七堂課:jQuery
sinmaplewing
0
2.1k
師大資工系ACM-ICPC讀書會:貳、排序搜尋與數學基礎
sinmaplewing
0
210
資訊實務應用讀書會 第六堂課:Basic CSS
sinmaplewing
0
1.8k
師大資工系ACM-ICPC讀書會:壹、字串處理與大數運算
sinmaplewing
0
170
師大資工系ACM-ICPC讀書會:零、比賽介紹、解題系統介紹
sinmaplewing
0
250
資訊實務應用讀書會 第四堂課(支線):Linux
sinmaplewing
0
1.9k
資訊實務應用讀書會 第一堂課:Basic HTML
sinmaplewing
1
2.2k
Other Decks in Programming
See All in Programming
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
340
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
3.1k
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
320
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
360
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
560
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
100
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
450
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.2k
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.3k
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
200
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
320
App Intentsのビルドプロセスを支える技術
kntkymt
0
480
Featured
See All Featured
Context Engineering - Making Every Token Count
addyosmani
9
1.2k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The Invisible Side of Design
smashingmag
301
52k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
440
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
My Coaching Mixtape
mlcsv
0
330
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
The Cost Of JavaScript in 2023
addyosmani
55
10k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
750
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Transcript
參、堆疊與佇列 師大資工系 灆洢 取自於<提升程式設計的邏輯思考力> 6.1
堆疊
•先進後出(FILO、LIFO) •push 1: 1 •push 5: 1 5 •push 3:
1 5 3 •push 4: 1 5 3 4 •pop: 1 5 3 // => 4 •push 6: 1 5 3 6 •pop: 1 5 3 // => 6 堆疊
• ex. • stack<int> aStack; • aStack.empty() // =>判斷堆疊是否為空 •
aStack.size() // =>目前堆疊大小 • aStack.push(2) // => 推入值進堆疊中 • aStack.pop() // => 移除堆疊最上方之值 (注意:不會回傳值) • aStack.top() // => 取得堆疊最上方之值 STL: stack<T>
•中序: 5 * (3 + 2) / (8 - 6)
//前中後 •前序: * 5 / + 3 2 - 8 6 // 中前後 •後序: 5 3 2 + * 8 6 - / // 前後中 前序、中序、後序 運算式
• ( ( 9 + 8 ) * 8 +
( ( 8 + 6 ) * ( 2 + 6 ) ) ) // => O • ( ( 9 + 8 ) * 8 + ( ( 8 + 6 * ( 2 + 6 ) ) ) // => X • 運用堆疊 • 遇左括弧推入堆疊中 • 遇右括弧推出左括弧 括弧檢查
• 中序: 5 * (3 + 2) / (8 -
6) //前中後 • 後序: 5 3 2 + * 8 6 - / // 前後中 • 從左而右,遇值即輸出。 • 遇運算子則檢查堆疊最上層運算子運算優先度是否高於自己 • 同運算優先度則越左邊優先度應越高 • 左括弧必定推入堆疊中,並得等右括弧出現才可推掉 中序式轉後序式
• 5 * (3 + 2) / (8 - 6)
// => 12 (or 12.5) • 利用運算元和運算子兩個堆疊來實作 • 與中序式轉後序式差不多 • 遇到運算元直接推入堆疊中 • 遇到運算子,跟中序式轉後序式一樣的判斷 • 若要推出則拿出兩個運算元計算完後堆回運算元的堆疊中 運算式運算
範例:Rails (UVa 514)
佇列
佇列 •先進先出(FIFO、LILO) •enqueue 1: 1 •enqueue 5: 1 5 •enqueue
3: 1 5 3 •enqueue 4: 1 5 3 4 •dequeue: 5 3 4 // => 1 •enqueue 6: 5 3 4 6 •dequeue: 3 4 6 // => 5
STL: queue<T> • ex. • queue<int> aQueue; • aQueue.empty() //
=>判斷佇列是否為空 • aQueue.size() // =>目前佇列大小 • aQueue.push(2) // => 推入值進佇列中 • aQueue.pop() // => 移除佇列最前方之值 (注意:不會回傳值) • aQueue.front() // => 取得佇列最前方之值 • aQueue.back() // => 取得佇列最後方之值
應用 • 最常被應用於BFS(廣度優先搜尋)當中 • 不過因為尚未講到樹與圖,所以先有印象就好 • 跟排隊有關的問題大抵上都要用到Queue • 作業系統的排程 •
Queueing Model • 作業系統、無線通訊、資料通訊課會上到XD (全部都是賀老師的課XD!!) • Event-Driven Programming (事件處理型程式設計)
範例:Throwing cards away I (UVa 10935)
合併應用
• 將佇列中的元素不再依照進入順序輸出,依照另外一種可計算之 順序重新輸出。 • enqueue 3: 3 • enqueue 1:
1 3 • enqueue 5: 1 3 5 • dequeue: 3 5 // => 1 • dequeue: 5 // => 3 • dequeue: // => 5 • 基本做法:利用二分搜尋找到該值應該插入的位置後插入。 • 可用陣列或串列實作 排序搜索+佇列=優先佇列(Priority Queue)
• ex. 預設是大值先出,可利用compare換 • priority_queue<int> pQueue; • pQueue.empty(); // =>
判斷是否為空 • pQueue.size(); // => 取得優先佇列中的大小 • pQueue.push(30); // =>推值進優先佇列中 • pQueue.pop(); // => 取得優先佇列中最大的值 STL: priority_queue<T(, container, compare)>
• 前後兩端皆可插入跟刪除 • Push Back 1: 1 • Push Back
3: 1 3 • Push Front 5: 5 1 3 • Push Front: 7: 7 5 1 3 • Pop Back: 7 5 1 // => 3 • Pop Front: 5 1 // => 7 堆疊+佇列=雙向佇列(Double-Ended Queue)
• ex. • deque<int> aDeque; • aDeque.empty(); // =>判斷是否為空 •
aDeque.size(); // => 得到目前大小 • aDeque.front(); // => 拿到最前方的值 • aDeque.back(); // => 拿到最後方的值 • aDeque.push_back(10); // => 增加值於後方 • aDeque.push_front(10); // => 增加值於前方 • aDeque.pop_back(); // => 移除最後方的值 • aDeque.pop_front(); // => 移除最前方的值 STL: deque<T>
•127 •101 •133 •10152 •673 作業 •442 •11111 •11234 •540
•10050
謝謝聆聽 資料取自於<提升程式設計的邏輯思考力> 6.1