Stack&Queue

ZSISC 32nd 鄭云晶

stack

  • 資料結構 (LIFO) 
  • 後進先出(Last In First Out)
  • 需引入函式庫<stack>

queue

  • 資料結構 (FIFO)
  • 先進先出(First In First Out)
  • 需引入函式庫<queue>

堆疊

佇列

push(x) push(x) 放入元素
pop() pop() 移除前端元素
top()​ front() 取得頂端/前端元素
empty() empty() 是否為空
size() ​size() 元素數量
X ​back() 取得尾端元素

stack

queue

常用函式

宣告:  stack/queue <型態> 名稱;

宣告

Stack

Queue

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

push

Stack

Queue

1

1

 s.push(1)

 q.push(1)

push

Stack

Queue

1

1

push

Stack

Queue

1

1

2

2

 s.push(2)

 q.push(2)

push

Stack

Queue

1

1

2

2

push

Stack

Queue

1

1

2

2

3

 s.push(3)

 q.push(3)

3

push

Stack

Queue

1

1

2

2

3

3

s.top()

q.front()

pop

Stack

Queue

1

1

2

2

3

3

 s.pop()

 q.pop()

pop

Stack

Queue

1

2

2

3

pop

Stack

Queue

1

2

2

3

 q.pop()

 s.pop()

pop

Stack

Queue

1

3

pop

Stack

Queue

1

3

 q.pop()

 s.pop()

pop

Stack

Queue

empty

Stack

Queue

回傳0

回傳1

size

Stack

Queue

回傳3

回傳0

1

2

3

範例

*空的時候不能 pop() 或 top()

#include <iostream>
#include <stack>
using namespace std;

int main(){
    stack<int> s;

    s.push(10);
    s.push(20);
    s.push(30);

    cout << s.top() << endl;

    s.pop();
    cout << s.top() << endl;
}
#include <iostream>
#include <queue>
using namespace std;

int main(){
    queue<int> q;

    q.push(10);
    q.push(20);
    q.push(30);

    cout << q.front() << endl; 

    q.pop();
    cout << q.front() << endl; 
}

10

20

30

20

題目練習

練習

排隊點餐

N 個人排隊點餐,每個人有一個號碼。
請模擬排隊順序,依照 先進先出(FIFO) 的方式輸出。

輸出

依照點餐順序輸出號碼。

輸入

第一行輸入一個整數 N
第二行輸入 N 個整數(代表排隊號碼)

 

答案

#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n;
    cin >> n;

    queue<int> q;

    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        q.push(x);
    }

    while(!q.empty()){
        cout << q.front() << endl;
        q.pop();
    }
}

練習

括號配對

輸入一串只包含 ( 和 ) 的字串,請判斷括號是否正確配對

如果配對正確輸出 YES,否則輸出 NO

輸出

YES 或 NO

輸入

一行字串,只包含 ( 和 )

配對規則:

* 每個 ( 必須有對應的 )

* 不能出現 ) 比 ( 先出現

答案

#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<char> s;
    string str;
    
    cin >> str;
    
    for(int i = 0; i < str.length(); i++){
        
        if(str[i] == '('){
            s.push('(');
        }
        
        else if(str[i] == ')'){
            if(s.empty()){
                cout << "NO";
                return 0;
            }
            s.pop();
        }
        
    }
    
    if(s.empty())
        cout << "YES";
    else
        cout << "NO";
        
    return 0;
}

q&p的邂逅

實作

#include <iostream>
#include <stack>
using namespace std;
int main(){

    stack <char> s;
    int n;
    int sum=0;   
    string pq;
    cin >> n;
   
    for(int i=0 ; i<n ; i++){
        
        cin >> pq;
              

        for(int j=0 ; j<pq.length() ; j++){
            
            if(pq[j] == 'p'){
                s.push(pq[j]);
            }
            else if(pq[j] == 'q' && !s.empty() ){
                sum = sum + 1;
                s.pop();
            }
            
        }
        
        cout << sum << endl;
        
        while(s.size() > 0){
            s.pop();
        }
        
    }
    

    return 0;
}

kahoot

stack&queue

By shiny0515

stack&queue

  • 32