Luke Tseng
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    4
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    【Python基礎教學】佇列 & 堆疊【part-15】 === 目錄(Table of Contents) [TOC] --- 哈囉大家好,很感謝你點進本文章,我是一名高中生,是電腦社社團的社長,由於我並不是 Python 這方面非常精通的專家,所以若文章有些錯誤請見諒,也可向我指正錯誤。另外本文章的用意是作為電腦社社團的教材使用而編寫的。 上次我們談到鏈結串列資料結構的概念,上次的 Python 實作上面作者將其定位為進階教學,不過本次所講的資料結構為佇列、堆疊這兩種,那他們的概念其實都蠻基礎蠻簡單的,在實作上也沒有像鏈結串列那麼複雜。 接下來,讓我們進入正題: 佇列(Queue) === 佇列(Queue),是一種線性的資料結構,具有先進先出(first in first out:FIFO)的特徵,特色為從一端插入資料至佇列(enqueue),然後在從另一端讀取資料(dequeue)。 ![image](https://hackmd.io/_uploads/ByfWQhdgC.png) Image Source:[Python Stack and Queue - Javatpoint](https://www.javatpoint.com/python-stack-and-queue) ![image](https://hackmd.io/_uploads/Sy2fXnOgC.png) Image Source:[Python Stack and Queue - Javatpoint](https://www.javatpoint.com/python-stack-and-queue) 上圖為 enqueue,表示插入資料進入佇列裡面,我們可以看到那個 D,在佇列中插入資料一定是從一端插入進去的。 下圖為 dequeue,表示在佇列中取出資料,我們看到 A,也是從一端中取出來。 看到這裡,你是不是覺得這像是什麼東西呢?是的沒錯,就是「排隊」等餐點的機制,因此 Queue 也稱為列隊、隊列的意思,簡單來說就是一個排好隊的隊伍。 欸,那這個佇列其實不一定要從左邊插入哦,其實你左右兩邊都可以,不過要注意的是,插入的這一端就已經被「插入」給佔用掉了,而取出資料的一端就必須要在插入的對面,以符合先進先出的原則。 那麼接下來就讓我們進入到 python 實作吧! Python 實作 --- 在 Python 中實作佇列,我們可以使用 list 來達成這個效果。enqueue 能夠用 list.insert(0,data) 來達成,而 dequeue 可以使用 list.pop() 方法。 ```python= class Queue(): def __init__(self): self.queue = [] def enqueue(self, data): self.queue.insert(0, data) def dequeue(self): if self.queue: return self.queue.pop() return "queue is empty" q = Queue() q.enqueue(1) q.enqueue(2) q.enqueue(3) print(q.dequeue()) print(q.dequeue()) print(q.dequeue()) print(q.dequeue()) ``` dequeue 方法主要是判斷佇列是否存在或是裡面元素不為空,不為空的話就從最後一個索引開始抽取,為空的話就回傳 "queue is empty"。 佇列模組 --- 在 python 中可以直接引入 queue 模組,然後在模組能夠直接使用 Queue() 物件。 ```python= from queue import Queue q = Queue() for i in range(3): q.put(i) while not q.empty(): print(q.get()) ``` 堆疊(stack) === 堆疊在我們之前講解遞迴時有稍微帶過他的基本原理,總之就是字面上意思,如果忘記的話各位可以回去翻遞迴那一篇。 堆疊(stack)也是一個線性資料結構,能夠由下往上堆積資料,就好比想像成你在上地科課的時候,老師教過一個定律叫做疊置定律,它是一種判斷事物年代久遠的方法,越下層的事物年代就越久遠,然後慢慢堆積上來。 ![image](https://hackmd.io/_uploads/B15nWLtlR.png) ![image](https://hackmd.io/_uploads/S1T2-UtlC.png) Image Source:[【Python】Stack(堆疊) 資料結構實作 | 愛喝咖啡 X 咖啡程式](https://lovedrinkcafe.com/python-stack-data-structure/) 將資料插入堆疊裡面的動作稱為堆入(push),堆進去堆疊裡面,肯定是由下往上嘛,如果我們要取出資料,那也因為堆疊的關係,只能從上面慢慢拿,所以堆疊具有 LIFO(last in first out)先進後出的特徵。 註:每個程式語言的遞迴式呼叫就是使用堆疊的原理。 Python 實作 --- 在 Python 實作堆疊,我們同樣也可以使用 list 來進行這個動作。那接下來我們會用到以下兩種方法: * append():在末端加入資料(符合堆疊原理,就像是在一個杯子裡面不斷丟東西,丟一丟最後索引還是一樣在後面) * pop():刪除末端資料(最先進去的會疊到最下面,所以索引為 0,後面堆到上面來的索引會增加) ```python= a = [] for i in range(5): a.append(i) print(*a) for i in range(5): print(a.pop()) ``` 以上是一個使用列表來達成簡易的堆疊程式碼。 那麼接下來讓我們像上面的範例一樣,自建一個類別來對 Stack 進行相關的操作: ```python= class Stack(): def __init__(self): self.stack = [] def push(self, data): self.stack.append(data) def pop(self): return self.stack.pop() stack = Stack() data = [1,2,3,4,5] for i in data: stack.push(data) print(f"Put {i} in stack") print(f'The stack length : {len(stack.stack)}') ``` 那堆疊實作的部分差不多就是這樣XD,是不是很簡單呢? 總結 === 1. 佇列(Queue): * 佇列是一種先進先出(FIFO)的資料結構,資料從一端插入(enqueue)並從另一端取出(dequeue)。這種結構類似於現實中的排隊情形。 * 可用 Python 的 list 實作佇列,insert(0, data) 用於插入資料,而 pop() 用於取出資料。 * Python 的 queue 模組內建了 Queue 類別,使用 put() 方法插入資料,get() 方法取出資料。 2. 堆疊(Stack): * 堆疊是一種先進後出(LIFO)的資料結構,資料像疊放的物品一樣,從上方堆入(push),並只能從上方取出(pop)。 * 可用 Python 的 list 來實作堆疊,append() 方法用於插入資料,而 pop() 方法用於取出資料。 * 也可自建 Stack 類別,push() 用於將資料加入堆疊,pop() 用於移除資料。 | 屬性 | 佇列(Queue) | 堆疊(Stack) | | -------- | -------- | -------- | | 結構類型 | 線性資料結構 | 線性資料結構 | | 插入方式 | 從一端插入(enqueue) | 從上方堆入(push) | | 取出方式 | 從另一端取出(dequeue) | 從上方取出(pop) | | 資料進出順序 | 先進先出(FIFO:First In First Out) | 先進後出(LIFO:Last In First Out) | | 現實類比 | 排隊等候 | 疊放的物品 | | Python 基本實作 | 用 `list.insert(0, data)` 和 `list.pop()` | 用 `list.append(data)` 和 `list.pop()` | | 內建模組 | `queue.Queue` | `collections.deque` 或自訂類別 | | 應用情況 | 點餐等候系統、伺服器請求處理 | 遞迴、復原操作(Undo Operation) | 復原操作就是常聽到的 ctrl + Z,就是一種堆疊的應用,比如說在文件編輯器上打上任何字,等同於把字堆入堆疊當中,想要復原時再把它取出來。 參考資料 === [用python 實作Queue(dequeue , enqueue) | by jack_DL | Information_Fun | Medium](https://medium.com/%E8%B3%87%E5%B7%A5%E7%AD%86%E8%A8%98/%E7%94%A8python-%E5%AF%A6%E4%BD%9Cqueue-dequeue-enqueue-e7f31da9543a) [Python Stack and Queue - Javatpoint](https://www.javatpoint.com/python-stack-and-queue) [【Python】Stack(堆疊) 資料結構實作 | 愛喝咖啡 X 咖啡程式](https://lovedrinkcafe.com/python-stack-data-structure/) 書籍:演算法:圖解邏輯思維+ Python 程式實作王者歸來(第三版)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully