1. 程式人生 > >C++ 知識回顧總結 -- queue 佇列容器

C++ 知識回顧總結 -- queue 佇列容器

一、說明

queue 是一種佇列介面卡,專門設計用於FIFO中操作(先進先出),元素從一端插入容器並從另一端提取。

相關API地址為:http://www.cplusplus.com/reference/queue/queue/

二、使用方法

在C++中只要#include<queue>即可使用佇列類,其中在面試或筆試中常用的成員函式如下(按照最常用到不常用的順序)

push、pop、size、empty、front、back

接下來逐一舉例說明:

1. push

佇列中由於是先進先出,push即在隊尾插入一個元素,如:

1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout<<q.front()<<endl;

可以輸出:Hello World!

2. pop

將佇列中最靠前位置的元素拿掉,是沒有返回值的void函式。如:

1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 q.pop();
5 cout<<q.front()<<endl;

可以輸出:China

原因是Hello World!已經被除掉了。

3. size

返回佇列中元素的個數,返回值型別為unsigned int。如:

queue<string> q;
cout<<q.size()<<endl;
q.push("Hello World!");
q.push("China");
cout<<q.size()<<endl;

輸出兩行,分別為0和2,即佇列中元素的個數。

4. empty

判斷佇列是否為空的,如果為空則返回true。如:

1 queue<string> q;
2 cout<<q.empty()<<endl;
3 q.push("Hello World!");
4 q.push("China");
5 cout<<q.empty()<<endl;

輸出為兩行,分別是1和0。因為一開始佇列是空的,後來插入了兩個元素。

5. front

返回值為佇列中的第一個元素,也就是最早、最先進入佇列的元素。注意這裡只是返回最早進入的元素,並沒有把它剔除出佇列。如:

複製程式碼
1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout<<q.front()<<endl;
5 q.pop();
6 cout<<q.front()<<endl;
複製程式碼

輸出值為兩行,分別是Hello World!和China。只有在使用了pop以後,佇列中的最早進入元素才會被剔除。

6. back

返回佇列中最後一個元素,也就是最晚進去的元素。如:

1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout<<q.back()<<endl;

輸出值為China,因為它是最後進去的。這裡back僅僅是返回最後一個元素,也並沒有將該元素從佇列剔除掉。

 三、使用連結串列將queue實現

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

template <typename T>
struct Node{
    T value;
    Node<T> *next;
    Node<T>(){next = NULL;}
};
 
template <typename T>
class MyQueue{
private:
    unsigned int num;
    Node<T> *first;
    Node<T> *last;
    
public:
    MyQueue();
    ~MyQueue();
    unsigned int size();
    void push(T element);
    void pop();
    bool empty();
    T back();
    T front();
};

template <typename T>
MyQueue<T>::MyQueue(){
    num = 0;
    first = NULL;
    last = NULL;
}

template <typename T>
MyQueue<T>::~MyQueue(){
    while(!empty()){
        pop();
    }
}

template <typename T>
unsigned int MyQueue<T>::size(){
    return num;
}

template <typename T>
bool MyQueue<T>::empty(){
    return (0==num);
}

template <typename T>
void MyQueue<T>::push(T element){
    Node<T> *temp = new Node<T>;
    temp->next = NULL;
    temp->value = element;
    if (0 == this->num){
        first = temp;
        last = temp;
    }else{
        last->next = temp;
        last = temp;
    }
    (this->num)++;
}

template <typename T>
void MyQueue<T>::pop(){
    if (0==this->num){
        cout<<"No elements in the queue!"<<endl;
    }else if(1 == this->num){
        delete first;
        first = NULL;
        last = NULL;
        this->num = 0;
    }else{
        Node<T> *temp = first;
        first = first->next;
        delete temp;
        (this->num)--;
    }
}

template <typename T>
T MyQueue<T>::back(){
    if (0==this->num){
        cout<<"No elements in the queue!"<<endl;
        return NULL;
    }
    return last->value;
}

template <typename T>
T MyQueue<T>::front(){
    if(0== this->num){
        cout<<"No elements in the queue!"<<endl;
        return NULL;
    }
    return first->value;
}

int main(){
    MyQueue<string> q;
    q.push("Hello world");
    q.push("China");
    cout<<q.front()<<endl;
    cout<<q.size()<<endl;
    cout<<q.back()<<endl;
    q.pop();
    cout<<q.empty()<<endl;
    cout<<q.back()<<endl;
    cout<<q.front()<<endl;
    q.pop();
    cout<<q.size()<<endl;
    cout<<q.empty()<<endl;
    system("pause");
    return 0;
}