iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
0
自我挑戰組

資料結構大便當系列 第 7

[Day 7] Stack,初入 Stack

Stack

Last-in, first-out (LIFO): 後進先出原則
https://ithelp.ithome.com.tw/upload/images/20190919/20120250cbtZgIsMED.png

基本操作可能有:
STACK-EMPTY

STACK-EMPTY(S)
    if S.top == -1
        return true;
    else 
        return false;

PUSH

PUSH(S, x)
    S.top++;
    S[S.top] = x;

POP

POP(S)
    if Stack-Empty(S)
        error underflow;
    else
        S.top--;
        return S[S.top+1];

https://ithelp.ithome.com.tw/upload/images/20190919/20120250wexgCTls3U.png


練習:Show how to implement a stack using two queues. Analyze the running time of the stack operations.

先將其中一個 queues 標記為啟用,並 PUSH 到啟動的 queues,如果要 POP 就將 queue 除了第一個元素以外,其他丟到另一個。並反轉 queue,再返回非啟用 queue 中的最後一個元素。


上一篇
[Day 6] pair 數對
下一篇
[Day 8] queue,初探 queue
系列文
資料結構大便當30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言