您的位置:首页 > 编程语言 > C语言/C++

C++ 知识回顾总结 -- queue 队列容器

2018-11-03 12:39 771 查看

一、说明

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

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: