博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
两个栈实现一个队列
阅读量:2725 次
发布时间:2019-05-13

本文共 918 字,大约阅读时间需要 3 分钟。

栈的特点:

先进后出
队列特点:
先进先出
实现方法:
定义两个栈,在插入队列的时候往栈1中插入,在删除的时候先把栈1的数据全部插入到栈2中,然后删除栈2顶部的元素,这样就可以实现先出的功能。

#define  _CRT_SECURE_NO_WARNINGS#include
using namespace std;#include
template
class Queue{public: void Push(const T& x) { _stack1.push(x); } void Pop() { if (_stack2.size() <= 0) { if (!_stack1.empty()) { T& data = _stack1.top(); _stack1.pop(); _stack2.push(data); } } if (_stack2.empty()) { cout << "queue is empty!" << endl; return; } _stack2.pop(); }private: stack
_stack1; stack
_stack2;};void test(){ Queue
q; q.Push(1); q.Push(2); q.Push(3); q.Push(4); q.Push(5); q.Pop(); q.Pop(); q.Pop(); q.Pop(); q.Pop();}int main(){ test(); system("pause"); return 0;}

转载地址:http://dqstd.baihongyu.com/

你可能感兴趣的文章
《STM:Video Object Segmentation using Space-Time Memory Networks》论文笔记
查看>>
《Non-local Neural Networks》论文笔记
查看>>
Pytorch中torch.nn.DataParallel负载均衡问题
查看>>
《FPGM:Filter Pruning via Geometric Median for Deep Convolutional Neural Networks Acceleration》论文笔记
查看>>
《Siamese RPN:High Performance Visual Tracking with Siamese Region Proposal Network》论文笔记
查看>>
《OpenPose:Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields》论文笔记
查看>>
《RETHINKING THE VALUE OF NETWORK PRUNING》论文笔记
查看>>
《Real-time 2D Multi-Person Pose Estimation on CPU:Lightweight OpenPose》论文笔记
查看>>
《Decoders Matter for Semantic Segmentation》论文笔记
查看>>
《HRNet:Deep High-Resolution Representation Learning for Human Pose Estimation》论文笔记
查看>>
《HigherHRNet:Scale-Aware Representation Learning for Bottom-Up Human Pose Estimation》论文笔记
查看>>
《PackNet:3D Packing for Self-Supervised Monocular Depth Estimation》论文笔记
查看>>
《EfficientNetV2:Smaller Models and Faster Training》论文笔记
查看>>
深度估计 DenseDepth 笔记
查看>>
《Structure-Guided Ranking Loss for Single Image Depth Prediction》论文笔记
查看>>
2017华为软挑——禁忌搜索算法
查看>>
Windows平台Socket通信实例
查看>>
高性能Socket模型
查看>>
C++虚函数原理和简单工厂模式
查看>>
C/C++面试知识点清理
查看>>