Stack

&Queue

 

Stack

&Queue

介紹

Stack(堆疊)

Stack         

  • 需引入函式庫<stack>
  • 為連續記憶體空間
  • 先進來的後出去(lifo)

Queue(佇列)

Queue   

  • 需引入函式庫<queue>
  • 為連續記憶體空間
  • 先進來的先出去(fifo)

Stack

Queue

stack<int> s;
queue<int> q;

s

q

stack/queue<型態>名稱;

Stack 功能
push(x) 放入元素
pop() 刪除第一個元素
top() 讀取最上面的元素
empty() 是否為空
size() 內有幾個元素
Queue 功能
push(x) 放入元素
pop() 刪除第一個元素
front() 讀取最前面的元素
empty() 是否為空
size() 內有幾個元素

是 否 為 空 ? 

     s.empty()

     q.empty()

內含幾個元素 ? 

     s.size()

     q.size()

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(1)

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(1)

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(2)

2

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(2)

2

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(3)

2

2

3

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(3)

2

2

3

s.top()

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(3)

2

2

3

s.top()

s.pop()

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(3)

2

2

3

s.top()

s.pop()

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(3)

2

2

3

s.pop()

Stack

lifo

(last-in-

first-out)

Stack

s

lifo

(last-in-

first-out)

1

s.push(3)

2

2

s.pop()

Queue

q

fifo(first-in-first-out)

q.push(3)

Queue

q

fifo(first-in-first-out)

q.push(1)

1

Queue

q

fifo(first-in-first-out)

q.push(1)

1

Queue

q

fifo(first-in-first-out)

q.push(2)

1

2

Queue

q

fifo(first-in-first-out)

q.push(2)

1

2

Queue

q

fifo(first-in-first-out)

q.push(3)

1

2

3

Queue

q

fifo(first-in-first-out)

q.push(3)

1

2

3

q.pop()

q.front()

Queue

q

fifo(first-in-first-out)

q.push(3)

1

2

3

q.pop()

q.front()

Queue

q

fifo(first-in-first-out)

q.push(3)

1

2

3

q.pop()

Queue

q

fifo(first-in-first-out)

q.push(3)

1

2

3

q.pop()

Queue

q

fifo(first-in-first-out)

q.push(3)

2

3

q.pop()

Stack

Queue

#include <iostream>
#include <stack>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    stack<型態>名字;
    //其餘程式碼
}
#include <iostream>
#include <queue>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    queue<型態>名字;
    //其餘程式碼
}

也可統一用#include <bits/stdc++.h>>

來試試:D

Q.Stack:

題目說明:

請利用迴圈在stack裡按順序放入1~9

並輸出最頂端的三個

測試輸入:

無輸入

測試輸出:

9

8

7

Q.Queue:

題目說明:

請利用迴圈在queue裡按順序放入1~9

並輸出最前方的三個

測試輸入:

無輸入

測試輸出:

1

2

3

來試試:D

A.Stack:

 

A.Queue:

 

#include <iostream>
#include <stack>

using namespace std;
int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);
    stack <int> s;
    for(int i=1;i<10;i++){
        s.push(i);
    }
    for(int i=0;i<3;i++){
        cout<<s.top()<<endl;
        s.pop();
    }
    return 0;
}
#include <iostream>
#include <queue>

using namespace std;
int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);
    queue <int> q;
    for(int i=1;i<10;i++){
        q.push(i);
    }
    for(int i=0;i<3;i++){
        cout<<q.front()<<endl;
        q.pop();
    }
    return 0;
}

優先佇列

Priorty_queue

優先佇列

Priorty_queue

Priority_queue

  • 會把加入的元素由大到小排列
  • 屬於queue的一種
  • 為連續記憶體空間

宣告方式be like:

priority_queue<型態> 名字;

Priotiry_queue 功能
push(x) 放入元素
pop() 刪除第一個元素
top() 讀取最上面的元素
empty() 是否為空
size() 內有幾個元素

Priority_queue

Priority_queue

pq

3

pq.push(3)

Priority_queue

pq

3

pq.push(3)

Priority_queue

pq

3

pq.push(2)

2

Priority_queue

pq

3

pq.push(2)

2

Priority_queue

pq

3

pq.push(2)

2

pq.top()

Priority_queue

pq

3

2

pq.top()

pq.pop()

Priority_queue

pq

3

2

pq.top()

pq.pop()

Priority_queue

pq

3

2

pq.top()

pq.pop()

Priority_queue

pq

2

pq.top()

pq.pop()

Priority_queue

對於英文等文字也有先後關係

a

c

b

->

->

->

97

99

98

Priority_queue

對於英文等文字也有先後關係

a

c

b

->

->

->

97

99

98

Priority_queue

對於英文等文字也有先後關係

abc

bbc

aabc

->

->

->

先看第一個字

97

97

98

Priority_queue

對於英文等文字也有先後關係

abc

bbc

aabc

->

->

->

先看第一個字

97

97

98

再看第二個字

Priority_queue

對於英文等文字也有先後關係

abc

bbc

aabc

->

->

->

先看第一個字

97

97

98

再看第二個字

Priority_queue

對於英文等文字也有先後關係

abc

bbc

aabc

->

->

->

先看第一個字

98

97

98

再看第二個字

Priority_queue

對於英文等文字也有先後關係

abc

bbc

aabc

->

->

->

先看第一個字

98

97

98

再看第二個字

Priority_queue

對於英文等文字也有先後關係

abc

bbc

aabc

->

->

->

先看第一個字

98

97

98

再看第二個字

來試試:D

Q.priority_queue:

題目說明:

輸入未按照大小排列的三個數字

並輸出最小的

測試輸入:

7 3 6

測試輸出:

3

來試試:D

A.priority_queue:

#include <iostream>
#include <queue>

using namespace std;
int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);
    priority_queue<int> pq;
    int in;
    for(int i=0;i<3;i++){
        cin>>in;
        pq.push(in);
    }
    while(pq.size()!=1){
        pq.pop();
    }
    cout<<pq.top()<<endl;
    return 0;
}

有沒有更簡單的方法呢?

當然

宣告時若以這樣宣告

則可將排序改為由小到大

priority_queue<型態,vector<型態>,greater<型態>> 名字;

當然

所以程式碼就會變成這樣

#include <iostream>
#include <queue>

using namespace std;
int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);
    priority_queue<int,vector<int>,greater<int>> pqg;
    int in;
    for(int i=0;i<3;i++){
        cin>>in;
        pqg.push(in);
    }
    cout<<pqg.top()<<endl;
    return 0;
}

卡戶!

Stack&Queue

By problemsolvemaster_kaitochiang