队列的链式实现
2015-06-28 10:14
302 查看
队列的链式实现:
![](http://images0.cnblogs.com/blog2015/640685/201506/281011570806381.jpg)
在这个队列里面:r 为低, f 为顶
![](http://images0.cnblogs.com/blog2015/640685/201506/281011570806381.jpg)
在这个队列里面:r 为低, f 为顶
//队列(链式) #include <iostream> using namespace std; typedef int DataType; struct QNode { DataType data; struct QNode *link; }; typedef struct QNode *PNode; //r为低 f为顶 struct LinkQueue { PNode f; PNode r; }; typedef struct LinkQueue * PLinkQueue; PLinkQueue createEmptyQueue() { PLinkQueue plqueue = (PLinkQueue) malloc (sizeof(struct LinkQueue)); if(plqueue != NULL) { plqueue ->f = NULL; plqueue ->r =NULL; } else printf("Out of space\n"); return plqueue; } int isEmptyQueue(PLinkQueue plqueue) { return (plqueue ->f == NULL); } void enQueue( PLinkQueue plqueue, DataType x) { PNode p = (PNode) malloc (sizeof(struct QNode)); if(p!= NULL) { p->data = x; p->link = NULL; if(plqueue ->f == NULL) plqueue->f = p; else plqueue ->r -> link = p; plqueue->r =p; } else printf("Out of space\n"); } void deQueue(PLinkQueue plqueue) { PNode p = (PNode) malloc (sizeof(struct QNode)); if(plqueue->f == NULL) printf("Empty Queue\n"); else { p = plqueue ->f; plqueue -> f = p ->link; free(p); } } DataType getFront(PLinkQueue plqueue) { if(plqueue ->f == NULL) printf("Empty Queue\n"); else return (plqueue ->f ->data); } int main() { PLinkQueue lqueue = createEmptyQueue(); cout<<"创建一个n元素的队列\n输入n"<<endl; int n,t; cin>>n; t = n; while(n) { int data; cin>>data; enQueue(lqueue,data); n--; } cout<<"取队头并出队"<<endl; while(t) { cout<<getFront(lqueue)<<" "; deQueue(lqueue); t--; } cout<<endl; system("pause"); return 0; }
相关文章推荐
- WPF编程—样式
- 优先级翻转与优先级继承
- Asp网页一些小问题释疑
- 我们该如何理解矩估计???
- Spring整合Strut2小结
- mongodb下载及安装配置教程【仅供参考】
- 2015062805 - EffactiveJava笔记 - 第41条 慎用重载(1)
- vim 命令集合
- 深度学习系列之ANN
- VS2012中配置OpenCV 2.4.9 死亡笔记
- Centos6 源码部署MySQL5.6
- Qt 出现“undefined reference to `vtable for”原因总结
- 使用eclipse编译和管理live555源码
- 数据挖掘-CART,C4.5,ID3,决策树,朴素贝叶斯
- jquery mobile 问问多多
- 【牛腩】网页验证码
- 折半查找
- 快速幂取余
- jsp显示java后台的异常
- 【Hadoop】7、Hadoop1.2.1基于虚拟机中的CentOS6.6的Hadoop单节点安装