在C++中只要#include<queue>即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序)

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 = ;
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 (==num);
} template <typename T>
void MyQueue<T>::push(T element){
Node<T> *temp = new Node<T>;
temp->next = NULL;
temp->value = element;
if ( == this->num){
first = temp;
last = temp;
}else{
last->next = temp;
last = temp;
}
(this->num)++;
} template <typename T>
void MyQueue<T>::pop(){
if (==this->num){
cout<<"No elements in the queue!"<<endl;
}else if( == this->num){
delete first;
first = NULL;
last = NULL;
this->num = ;
}else{
Node<T> *temp = first;
first = first->next;
delete temp;
(this->num)--;
}
} template <typename T>
T MyQueue<T>::back(){
if (==this->num){
cout<<"No elements in the queue!"<<endl;
return NULL;
}
return last->value;
} template <typename T>
T MyQueue<T>::front(){
if(== 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 ;
}

队列实现

[C++基础]队列<queue>中的常用函数的更多相关文章

  1. 学习笔记之Java队列Queue中offer/add函数,poll/remove函数,peek/element函数的区别

    队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用. Java中Que ...

  2. socket编程中客户端常用函数

    1 常用函数 1.1   connect() int connect(int sockfd, const struct sockaddr *servaddr, socklen_taddrlen); 客 ...

  3. Greenplum入门——基础知识、安装、常用函数

    Greenplum入门——基础知识.安装.常用函数 2017年10月08日 22:03:09 在咖啡里溺水的鱼 阅读数:8709    版权声明:本文为博主原创,允许非商业性质转载但请注明原作者和出处 ...

  4. Mysql中的常用函数:

    Mysql中的常用函数: 1.字符串函数: (1).合并字符串 concat():// concat('M','y',"SQL",'5.5');== MySQL5.5//当传入的参 ...

  5. numpy函数库中一些常用函数的记录

    ##numpy函数库中一些常用函数的记录 最近才开始接触Python,python中为我们提供了大量的库,不太熟悉,因此在<机器学习实战>的学习中,对遇到的一些函数的用法进行记录. (1) ...

  6. 日期时间函数 mysql 和sqlserver 中对于常用函数的日期和时间函数的区别

    1. sqlserver中获取时间用getdate(),默认返回格式是2019-01-21 13:58:33.053,具体的年月日,时分秒毫米,年月日之间用短线连接,时分秒之间用冒号连接,秒和毫米之间 ...

  7. 算法与数据结构基础 - 队列(Queue)

    队列基础 队列具有“先进先出”的特点,用这个特点我们可以用它来处理时间序列相关或先后次序相关的问题,例如 LeetCode题目 933. Number of Recent Calls,时间复杂度O(1 ...

  8. MySQL基础篇(3)常用函数和运算符

    一.字符串函数(索引位置都从1开始) CONCAT(S1,S2,...Sn): 连接S1,S2,...Sn为一个字符串,任何字符串与NULL进行连接的结果都是NULL INSERT(str,x,y,i ...

  9. C++中string常用函数用法总结

    string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<string>,注意不是<string.h>,下面记录一下string中比较常用的 ...

随机推荐

  1. spring 的 ApplicationContext.getBean(type) 无法获取bean,报错

    具体问题请看   https://q.cnblogs.com/q/108101/ 研究了两天: 经过上文中的排除法: 造成问题的原因是要获取的bean 中 有被切入的方法.. 就是可能该类会使用反射生 ...

  2. netty4初步使用

    文件 D:\jp\netty\NtServer.java import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Chan ...

  3. centos下安装配置jetty

    下载jdk-8u144-linux-x64.tar.gz # tar -zxvf jdk-8u144-linux-x64.tar.gz # mv jdk1.8.0_144 /usr/java/ # u ...

  4. C# 遇到 which has a higher version than referenced assembly

    当C#遇到这种提示: which has a higher version than referenced assembly, 说明有两个或多个工程引用的dll的版本有出现不一样, 如: A工程引用l ...

  5. TFS SDK

    vs2013 已包含. 可参考 TFS SDK: Connecting to TFS 2010 & TFS 2012 Programmatically http://geekswithblog ...

  6. 【Linux】 Ncures库的介绍与安装

    Ncures库的介绍 ncurses(new curses)是一套编程库,它提供了一系列的函数以便使用者调用它们去生成基于文本的用户界面. ncurses名字中的n意味着“new”,因为它是curse ...

  7. apm飞行模式

    参考 :https://www.cnblogs.com/jins-note/p/9580054.html   复制别人的,因为很久(几年)玩一次,所以会忘,也不好找,,若作者要求,请给留言,会立即删除 ...

  8. Office2013 如何安装Matlab notebook

    Office2013 如何安装Matlab notebook 听语音 浏览:912 | 更新:2014-09-16 07:02 1 2 3 4 5 6 7 分步阅读 Office2013(64bit) ...

  9. IntellJ IDEA 2017 激活编译器配置,读取多个配置文件

    1.设置编译器,找到1,点击2 2.输入设置命令:--spring.profiles.active=test,如果是多个文件输入--spring.profiles.active=test,dev 3. ...

  10. 根据方法名获取方法Body Content

    利用 MethodBody类的GetILAsByteArray方法可以获取到返回字节数组的MSIL的body.然后再去解析此字节数组, 可以得到MSIL,然后你再去解析MSIL,你就可以得到你想到so ...