[C++基础]队列queue中的常用函数
本博客转载自:https://www.cnblogs.com/xuning/p/3321733.html
在C++中只要#include即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序)
1. push()
2. pop()
3. size()
4. empty()
5. front()
6. back()
接下来逐一举例说明:
1. push()
队列中由于是先进先出,push即在队尾插入一个元素,如:
queue<string> q;
q.push("Hello World!");
q.push("China");
cout<<q.front()<<endl;
可以输出:Hello World!
2. pop()
将队列中最靠前位置的元素拿掉,是没有返回值的void函数。如:
queue<string> q;
q.push("Hello World!");
q.push("China");
q.pop();
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。如:
queue<string> q;
cout<<q.empty()<<endl;
q.push("Hello World!");
q.push("China");
cout<<q.empty()<<endl;
输出为两行,分别是1和0。因为一开始队列是空的,后来插入了两个元素。
5. front()
返回值为队列中的第一个元素,也就是最早、最先进入队列的元素。注意这里只是返回最早进入的元素,并没有把它剔除出队列。如:
queue<string> q;
q.push("Hello World!");
q.push("China");
cout<<q.front()<<endl;
q.pop();
cout<<q.front()<<endl;
输出值为两行,分别是Hello World!和China。只有在使用了pop以后,队列中的最早进入元素才会被剔除。
6. back()
返回队列中最后一个元素,也就是最晚进去的元素。如:
queue<string> q;
q.push("Hello World!");
q.push("China");
cout<<q.back()<<endl;
输出值为China,因为它是最后进去的。这里back仅仅是返回最后一个元素,也并没有将该元素从队列剔除掉。
其他的方法不是很常用,就不再研究了。
接下来我们使用链表,自己将queue类写出来,将其所有方法都实现。代码都是自己写的,最后随便写了点main函数小测一下,没有发现问题,如果有不足还望能指正。如下:
#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;
}
队列实现