[C++基礎]隊列 中的常用函數


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

1. push

2. pop

3. size

4. empty

5. front

6. 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類寫出來,將其所有方法都實現。代碼都是自己寫的,最后隨便寫了點main函數小測一下,沒有發現問題,如果有不足還望能指正。如下:

  1 #include<iostream>
  2 #include<string>
  3 using namespace std;
  4 
  5 template <typename T>
  6 struct Node{
  7     T value;
  8     Node<T> *next;
  9     Node<T>(){next = NULL;}
 10 };
 11  
 12 template <typename T>
 13 class MyQueue{
 14 private:
 15     unsigned int num;
 16     Node<T> *first;
 17     Node<T> *last;
 18     
 19 public:
 20     MyQueue();
 21     ~MyQueue();
 22     unsigned int size();
 23     void push(T element);
 24     void pop();
 25     bool empty();
 26     T back();
 27     T front();
 28 };
 29 
 30 template <typename T>
 31 MyQueue<T>::MyQueue(){
 32     num = 0;
 33     first = NULL;
 34     last = NULL;
 35 }
 36 
 37 template <typename T>
 38 MyQueue<T>::~MyQueue(){
 39     while(!empty()){
 40         pop();
 41     }
 42 }
 43 
 44 template <typename T>
 45 unsigned int MyQueue<T>::size(){
 46     return num;
 47 }
 48 
 49 template <typename T>
 50 bool MyQueue<T>::empty(){
 51     return (0==num);
 52 }
 53 
 54 template <typename T>
 55 void MyQueue<T>::push(T element){
 56     Node<T> *temp = new Node<T>;
 57     temp->next = NULL;
 58     temp->value = element;
 59     if (0 == this->num){
 60         first = temp;
 61         last = temp;
 62     }else{
 63         last->next = temp;
 64         last = temp;
 65     }
 66     (this->num)++;
 67 }
 68 
 69 template <typename T>
 70 void MyQueue<T>::pop(){
 71     if (0==this->num){
 72         cout<<"No elements in the queue!"<<endl;
 73     }else if(1 == this->num){
 74         delete first;
 75         first = NULL;
 76         last = NULL;
 77         this->num = 0;
 78     }else{
 79         Node<T> *temp = first;
 80         first = first->next;
 81         delete temp;
 82         (this->num)--;
 83     }
 84 }
 85 
 86 template <typename T>
 87 T MyQueue<T>::back(){
 88     if (0==this->num){
 89         cout<<"No elements in the queue!"<<endl;
 90         return NULL;
 91     }
 92     return last->value;
 93 }
 94 
 95 template <typename T>
 96 T MyQueue<T>::front(){
 97     if(0== this->num){
 98         cout<<"No elements in the queue!"<<endl;
 99         return NULL;
100     }
101     return first->value;
102 }
103 
104 
105 int main(){
106     MyQueue<string> q;
107     q.push("Hello world");
108     q.push("China");
109     cout<<q.front()<<endl;
110     cout<<q.size()<<endl;
111     cout<<q.back()<<endl;
112     q.pop();
113     cout<<q.empty()<<endl;
114     cout<<q.back()<<endl;
115     cout<<q.front()<<endl;
116     q.pop();
117     cout<<q.size()<<endl;
118     cout<<q.empty()<<endl;
119     system("pause");
120     return 0;
121 }
隊列實現

 


免責聲明!

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



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