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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM