队列操作的实现-链式存储
来源:互联网 发布:电脑智能机器人软件 编辑:程序博客网 时间:2024/05/16 06:02
#include <iostream>#include <stdio.h>#include <malloc.h>using namespace std;//链式队列结点typedef struct Node{ int data; struct Node *next;}Node;//队列typedef struct { Node *front,*rear;}LinkQueue;//初始化void init(LinkQueue *q){ q->front=(Node*)malloc(sizeof(Node)); if(q->front!=NULL){ q->rear=q->front; q->front->next=NULL; }}//判空bool isempty(LinkQueue *q){ if(q->front==q->rear) return true; else return false;}//入队void enqueue(LinkQueue *q,int e){ //创建新结点,插入到链尾 Node *s=(Node *)malloc(sizeof(Node)); s->data=e; s->next=NULL; printf("%d 入队成功.\n",e); q->rear->next=s; q->rear=s;//修改队尾指针}//出队void dequeque(LinkQueue *q){ if(q->front==q->rear) printf("empty"); int x; Node *s=q->front->next; x = s->data; printf("%d 出队.\n",x); q->front->next=s->next; if(q->rear==s) q->rear=q->front;//若原队列中只有一个节点,删除后为空 s=NULL;}//取队头元素int gettop(LinkQueue *q){ int x; if(q->front==q->rear) return 0; else x=q->front->next->data; return x;}//输出void show(LinkQueue *q){ while(!isempty(q)){ cout<<q->front->next->data<<" "; q->front->next=q->front->next->next; } cout<<endl;}int main(){ LinkQueue q; LinkQueue *s; s=&q; //初始化队列 init(s); //入队 for(int i=0;i<5;i++) enqueue(s,i); //出队 dequeque(s); //取队头元素 int x = gettop(s); cout<<"队头元素是:"<<x<<endl; return 0;}
阅读全文
0 0
- 队列操作的实现-链式存储
- JAVA实现队列 队列的链式存储结构及操作
- 队列的链式存储操作
- 队列的链式存储的实现及操作
- 链式队列存储实现及操作
- 链式队列的操作实现
- 队列的链式存储结构与操作
- 队列的链式存储结构与操作
- 队列的链式存储相关操作
- 队列链式存储实现
- 队列的链式存储结构C++实现
- 循环队列的链式存储实现
- 队列的链式存储结构及实现
- 队列的链式存储结构及实现
- 队列的链式存储实现c语言
- 链式队列的存储....
- 队列的链式存储
- 队列的链式存储
- [bzoj4176]Lucas的数论
- 一步步搭建Retrofit+RxJava+MVP网络请求框架(一)
- 遍历list时删除元素的正确做法
- 关于HTTP
- QT控件大全 二十八 QMultiYearCalender
- 队列操作的实现-链式存储
- LintCode 第423题
- Model Tree Structures with Nested Sets
- 阿里云前端周刊
- 2018年才是人工智能元年?今年不是!
- mysql大表优化方案
- 理解朴素贝叶斯
- LinearLayout之weight完美详解
- ftp文件上传和下载