zl程序教程

您现在的位置是:首页 >  其他

当前栏目

数据结构复习之用两个栈模拟队列操作

模拟队列数据结构队列 操作 两个 复习 之用
2023-09-14 08:57:55 时间
struct Queue{//这种实现方式也是最容易想到的 Stack s1, s2;//s1用于队列的push和pop, s2用于是的缓冲(将s1中的数据进行反转,就成了队列的顺序) bool push(int x){ if(s1.stackOverFlow()) return false; int e; while(!s1.isEmpty()){ s1.pop(e); s2.push(e); s1.push(x); while(!s2.isEmpty()){ s2.pop(e); s1.push(e); return true; bool pop(int x){ if(s1.isEmpty()) return false; s1.pop(x); return true; bool isEmpty(){ return s1.size() == 0 ? true : false; int size(){ return s1.size(); struct Queue_x{//这种方式的空间利用率更大一些 Stack s1, s2;//s1用于队列的push, s2用于队列的pop bool push(int x){ if(!s1.stackOverFlow()) { s1.push(x); return true; if(s1.stackOverFlow() !s2.isEmpty()) return false; int e; while(!s1.isEmpty()){ s1.pop(e); s2.push(e); s1.push(x); return true; bool pop(int x){ if(!s2.isEmpty()){ s2.pop(x); return true; if(s1.isEmpty()) return false; int e; while(!s1.isEmpty()){ s1.pop(e); s2.push(e); s2.pop(x); return true; bool isEmpty(){ return s1.size() == 0 s2.size() == 0; int size(){ return s1.size() + s2.size(); int main(){ Queue q; for(int i=0; i ++i) q.push(i); int x; for(int i=0; i ++i){ q.pop(x); cout x endl; q.push(100); cout "队列的当前大小:" q.size() endl; while(!q.isEmpty()){ q.pop(x); cout x endl; cout "******************************************************" endl; Queue_x qx; for(int i=0; i ++i) qx.push(i); for(int i=0; i ++i){ qx.pop(x); cout x endl; qx.push(100); cout "队列的当前大小:" qx.size() endl; while(!qx.isEmpty()){ qx.pop(x); cout x endl; return 0; }
数据结构(严蔚敏版)第三章——栈和队列(三)【队列的表示和操作的实现】 队列(Queue)是仅在表尾进行插入操作,在表头进行删除操作的线性表 表尾即an端,称为队尾;表头即a1端,称为队头。它是一种先进先出(FIFO)的线性表;插入元素称为入队;删除元素称为出队、队列的存储结构为链队或顺序 3.4、栈与递归 3.4.1、采用递归算法解决的问题 3.5、队列的表示和操作的实现 3.5.1、相关术语 3.5.2、队列的相关概念 3.5.3、队列的类型定义 3.5.4、队列的顺序表示和实现 3.5.5、队列的链式表示和实现