[C++基础]队列<queue>中的常用函数

在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 }
队列实现

 

posted on 2013-09-14 20:26  Horstxu  阅读(89021)  评论(0编辑  收藏  举报

导航