队列的链式表示和实现
来源:互联网 发布:中序遍历非递归算法 编辑:程序博客网 时间:2024/05/22 17:07
- 队列的链式表示和实现
- 16年3月2日19:56:24
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- typedef struct Qnode {
- int data;
- struct Qnode *pNext;
- }QNODE, *PQNODE;
- typedef struct {
- PQNODE front;
- PQNODE rear;
- }LinkQueue;
- /**
- * Initialise the Queue. Attention : return the address of the Queue. Don't use the void .
- * @param Q [the LinkQueue]
- * @return [if success, return the address of the queue]
- */
- LinkQueue * InitQueue (LinkQueue *Q)
- {
- Q->front = Q->rear = (PQNODE)malloc(sizeof(QNODE));
- if (!Q->front || !Q->rear)
- {
- printf("Cannot malloc memory for Q->fornt or Q->rear.\n");
- exit(-1);
- }
- Q->front->pNext = NULL;
- return Q;
- }
- /**
- * Insert the entry to the front of the queue.
- * @param Q [the LinkQueue]
- * @param val [the value of the entry]
- * @return [if success, return 1]
- */
- int EnQueue(LinkQueue *Q, int val)
- {
- PQNODE pTmp;
- pTmp = (PQNODE)malloc(sizeof(QNODE));
- if (!pTmp)
- {
- printf("Can not malloc memory for pTmp.\n");
- exit(-1);
- }
- pTmp->data = val;
- pTmp->pNext = NULL;
- Q->rear->pNext = pTmp;
- Q->rear = pTmp;
- return 1;
- }
- /**
- * Judge the LinkQueue is empty or not.
- * @param Q [the LinkQueue]
- * @return [if the linkqueue is empty, return 1, else return 0]
- */
- int IsQueueEmpty(const LinkQueue *Q)
- {
- return Q->front == Q->rear;
- }
- /**
- * Remove the entry from the rear of the linkqueue.
- * @param Q [the linkqueue]
- * @param val [the val of the removed entry]
- * @return [if success, return 1]
- */
- int DeQueue(LinkQueue *Q, int *val)
- {
- PQNODE pTmp;
- if (IsQueueEmpty(Q))
- {
- printf("The Queue is empty.\n");
- exit(-1);
- }
- pTmp = Q->front->pNext;
- *val = pTmp->data;
- Q->front->pNext = pTmp->pNext;
- free(pTmp);
- return 1;
- }
- /**
- * Ruturn the length of the linkqueue.
- * @param Q [the linkqueue]
- * @return [the length]
- */
- int TheLengthOfQueue(LinkQueue Q)
- {
- int i = 0;
- PQNODE pTmp;
- pTmp = Q.front;
- while (pTmp != Q.rear)
- {
- i++;
- pTmp = pTmp->pNext;
- }
- return i;
- }
- /**
- * Traverse the linkqueue.
- * @param Q [the linkqueue]
- */
- void TraverseQueue(LinkQueue Q)
- {
- PQNODE pTmp = Q.front->pNext;
- while (pTmp)
- {
- printf("%d ", pTmp->data);
- pTmp = pTmp->pNext;
- }
- printf("\n");
- }
- int main(int argc, char const *argv[])
- {
- LinkQueue Q;
- int val;
- InitQueue(&Q);
- EnQueue(&Q, 1);
- EnQueue(&Q, 2);
- EnQueue(&Q, 3);
- EnQueue(&Q, 4);
- TraverseQueue(Q);
- printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));
- if (DeQueue(&Q, &val))
- {
- printf("DeQueue success, the val is %d.\n", val);
- }
- if (DeQueue(&Q, &val))
- {
- printf("DeQueue success, the val is %d.\n", val);
- }
- printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));
- return 0;
- }
- P { margin-bottom: 0.21cm; }
程序运行结果是:
12 3 4
TheLengthOfQueueis 4.
DeQueuesuccess, the val is 1.
DeQueuesuccess, the val is 2.
TheLengthOfQueueis 2.
0
上一篇:栈的应用之行编辑程序
下一篇:sublime text 3103从新安装中ctags配置
相关热门文章
- SHTML是什么_SSI有什么用...
- 卡尔曼滤波的原理说明...
- shell中字符串操作
- 关于java中的“错误:找不到或...
- linux设备驱动归纳总结...
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
0 0
- 队列的链式表示和实现
- 链式队列的表示和实现
- 链式队列的表示和实现
- 单队列的链式表示和实现
- 队列的链式表示和实现
- 队列的链式表示和实现
- 队列的链式表示和实现(数据结构)
- 队列的链式表示和实现
- 队列的链式表示和实现
- 链队列——队列的链式表示和实现
- 链队列-队列的链式表示和实现
- 链队列--队列的链式表示和实现
- 队列的链式表示和实现----单链队列
- 队列的链式表示和实现----循环队列
- 链队列---队列的链式表示和实现
- 数据结构之队列的链式表示和实现
- 队列的链式表示与实现
- 队列的链式表示与实现
- 顺序栈的操作
- 用RecyclerView实现新闻列表页,包括头部的图片轮播,两种Item显示方式,下拉刷新和上拉加载以及限制列表的加载条目数
- 栈的应用之数制转换
- 栈的应用之括号匹配的检验
- 栈的应用之行编辑程序
- 队列的链式表示和实现
- 当RelativeLayout放在Scrolling容器中,手动调用Measure方法出现空指针
- 编程学习网freecodecamp
- 修改android开机动画
- LayoutParams——LinearLayout.LayoutParams
- sublime text 3103从新安装中ctags配置
- 64位系统安装32位交叉工具链时No such file or directory的解决办法
- 启动根文件系统时提示WARNING: Unable to open an initial console的解决办法
- 开发板设置从nfs启动
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
李庄浩背后的保护伞
李莊
李庄镇
李庆远
李庆
李庆雄
李庆军
李庆萍
李庆祥
李庆奎出事
李庆国
荆门李庆华
李庆誉
李庆奎被留置
李庆奎之子李翔宇
罗来友贿赂李庆奎儿子
李应
李家老店作品
李二鲜鱼火锅店
李店
李庚
李度
李庭
神雕李庭
神雕游侠李庭
李延
李建
何生李建
小城李建
何生小城李建
一生有你 李建
车站歌词李建
父亲原唱是谁
父亲原唱
假如爱有天意歌曲原唱
假如爱有天意原唱
父亲歌曲原唱
父亲谁是原唱
父亲的散文诗李建
李弘
李弘怎么死的