資料結構
Algorithms + Data Structures = Programs
- by Niklaus Wirth
資料結構就是一種有效儲存資料的方式,使得你可以在特定複雜度的時間與空間內取出資料,並利用它的特性來解決麻煩的問題
Stack (棧)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
後進去的人先出來,稱之LIFO(Last In First Out)
實作
一個陣列+一個變數紀錄stack尾部
這樣就是一個優雅的Stack了
事實上C++98開始就有提供stack這個STL容器了
可以#include <stack>
來使用
複雜度
Queue(佇列)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
先進去的人先出來,稱之FIFO(First In First Out)
實作
用陣列很不方便
我自己個人會偏好使用雙向Linked List來實作
複雜度
Linked List (串列)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
「真像人形蜈蚣啊。」一位外號叫「大食客」的同學緊接著說。
結構如圖所示,有著快速於尾端插入移除與存取的特性,也可以輕鬆地從中間移除某個元素,缺點就是隨機存取(任意存取任意位置的元素)的速度很慢
實作
真的要我實作喔,我好懶喔
懶得刻移除了XDD
但概念與插入相仿
一樣要記得好好處理麻煩的飛來飛去的指標
還有new
出來的東西一定要delete
掉(不然等等記憶體洩漏我坐等你MLE)
幾本上就沒什麼問題了