队列,链队列,循环队列
来源:互联网 发布:a5淘宝客论坛 编辑:程序博客网 时间:2024/05/11 23:08
队列:当然用c++
#include<bits/stdc++.h>int main(){ std::queue<int> q;int x; while(scanf("%d",&x),x) q.push(x); std::cout<<q.size()<<"\n"; while(!q.empty()) std::cout<<q.front()<<'\n',q.pop(); return 0;}
链队列:c语言实现
#include<bits/stdc++.h>using namespace std;typedef int QElemType;typedef struct node{ QElemType data; struct node *next;}QNode,*QueuePtr;typedef struct{ QueuePtr front; QueuePtr rear;}LinkQueue;void InitQueue(LinkQueue &Q)//初始化队列{ Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));//先申请内存 if(!Q.front) exit(0); Q.front->next=NULL;//下一个节点为空}void DestoryQueue(LinkQueue &Q)//销毁队列{ while(Q.front)//循环销毁 { Q.rear=Q.front->next; free(Q.front); Q.front=Q.rear; }}void EnQueue(LinkQueue &Q,QElemType e)//插入队尾元素{ QNode *p; p=(QueuePtr)malloc(sizeof(QNode)); if(!p) exit(0); p->data=e; p->next=NULL; Q.rear->next=p;//自己画图理解 Q.rear=p;}QElemType DeQueue(LinkQueue &Q){ QElemType e; QNode *p; if(Q.front==Q.rear) exit(0); p=Q.front->next; e=p->data; Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; free(p); return e;}bool QueueEmpty(LinkQueue &Q){ if(Q.front==Q.rear) return true; else return false;}int main(){ LinkQueue Q; InitQueue(Q); int x; while(scanf("%d",&x),x) EnQueue(Q,x); while(!QueueEmpty(Q)) cout<<DeQueue(Q)<<endl; return 0;}
循环队列
#include<bits/stdc++.h>using namespace std;#define MAXQSIZE 100typedef int QElemType;typedef struct{ QElemType *base; int front; int rear;}SqQueue;void InitQueue(SqQueue &Q){ Q.base=(QElemType *)malloc(MAXQSIZE * sizeof(QElemType)); if(!Q.base) exit(0); Q.front=Q.rear=0;}int QueueLength(SqQueue Q){ return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;}void EnQueue(SqQueue &Q,QElemType e){ if((Q.rear+1)%MAXQSIZE==Q.front) exit(0); Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXQSIZE;}int DeQueue(SqQueue &Q){ QElemType e; if(Q.front==Q.rear) exit(0); e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXQSIZE; return e;}bool QueueEmpty(SqQueue Q){ if(Q.front==Q.rear) return true; else return false;}int main(){ SqQueue Q; InitQueue(Q); QElemType x; while(scanf("%d",&x),x) EnQueue(Q,x); cout<<QueueLength(Q)<<endl; return 0;}
0 0
- 队列-循环队列/链队列
- 队列,链队列,循环队列
- 队列,链队列,循环队列
- 队列---循环队列与链队列比较
- 顺序队列 - 循环队列 - 链队列
- 循环队列和链队列
- 循环队列和链队列
- 关于队列(数组队列,链表队列,循环队列)
- 队列--数组循环队列
- 队列 -- 循环队列
- 循环队列和队列
- 队列之循环队列
- 队列-静态循环队列
- 【数据结构】队列-循环队列
- 队列之循环队列
- 【数据结构-队列】循环队列
- 队列---循环队列
- 【队列】循环队列
- 使用Python脚本来导入CVE数据到MySQL
- python调用bat脚本重启appium
- STL-queue(队列)
- Toast导致Activity内存泄露的解决方法
- Linux Mint:MySQL的安装
- 队列,链队列,循环队列
- CocoaPods的安装,升级以及升级以后出现bug的解决方法(升级必看!)
- MySQL数据库自增主键归零的几种方法
- 【数据结构与算法】桶排序
- 《疯狂Android讲义》 -- Android 动画系列之补间(Tween)动画
- SBT - Resolvers
- $.ajax的beforeSend,success, complete,error例子
- 有关UIScrollView zoom的一点心得
- Yii2使用Bootbox插件自定义弹窗