队列<queue>中的常用函数
来源:互联网 发布:淘宝联盟提现没到账 编辑:程序博客网 时间:2024/06/07 14:29
转载自:https://www.cnblogs.com/xuning/p/3321733.html
在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 }
- 队列<queue>中的常用函数
- 队列<queue>中的常用函数
- [C++基础]队列<queue>中的常用函数
- [C++基础]队列<queue>中的常用函数
- 队列queue中的常用函数 c++ 容器
- 队列<queue>中常用的函数
- 栈stack和队列queue的常用基本函数
- java中的队列Queue
- java中的队列-Queue
- 队列中的Queue与Deque
- Unity常用数据结构Queue(队列)
- Linux中的工作队列(work queue)
- java中的queue(队列)的使用
- Java中的队列API——Queue
- Java中的队列(Queue)简单使用
- JS中的队列(queue)和栈(stack)
- Java中的队列API——Queue
- C++中的队列(queue)和优先队列(priority_queue)总结
- 我始终以为这是下限
- 【Python语法笔记】range()函数
- The CATALINA_HOME environment variable is not defined correctly
- Java里的堆(heap)栈(stack)和方法区(method)
- lesson8:集合框架作业2
- 队列<queue>中的常用函数
- 阶段性阅读源代码
- Java互联网架构-京东国美高并发核心技术“秒杀”
- 第十三课关卡创建1 简介
- window.onload=init;
- USART的最高通信速率
- 负载均衡原理的解析
- eclipse 修改工程名
- pdoFetch参数