队列
来源:互联网 发布:高考心态调整 知乎 编辑:程序博客网 时间:2024/05/20 21:47
<span style="font-size:18px;">#include <stdio.h>#include <stdlib.h>struct QNode{ int data; struct QNode* next;};struct Queue{ struct QNode* front; struct QNode* rear;}; struct Queue* init();void destroy(struct Queue ** pq);int EnQ(struct Queue* q, int item);int DeQ(struct Queue* q, int *pitem); struct Queue* init(){ struct Queue* tmp; tmp = (struct Queue*)malloc(sizeof(struct Queue)); if(!tmp) return NULL; tmp->front = tmp->rear = NULL; return tmp;}void destroy(struct Queue ** pq){ if (*pq == NULL)return; struct QNode *tmp, *head; head = (*pq)->front; while (head) { tmp = head; head = head->next; free(tmp); } free(*pq);}int EnQ(struct Queue* q, int item){ struct QNode* tmp; tmp = (struct QNode*)malloc(sizeof(struct QNode)); if (!tmp) return 0; tmp->data = item; tmp->next = NULL; if (q->front) { q->rear->next = tmp; q->rear = tmp; }else{ q->front = q->rear = tmp; } return 1;}int DeQ(struct Queue* q, int *pitem){ if (q->front == NULL) return 0; struct QNode* tmp; tmp = q->front; *pitem = tmp->data; q->front = tmp->next; free(tmp); if (q->front == NULL) q->rear = NULL; return 1;} int main(){ struct Queue *q; q = init(); int item; EnQ(q,3); EnQ(q,5); DeQ(q,&item); printf("%d\n",item); EnQ(q,2); EnQ(q,1); EnQ(q,0); EnQ(q,9); DeQ(q,&item); printf("%d\n",item); DeQ(q,&item); printf("%d\n",item); DeQ(q,&item); printf("%d\n",item); DeQ(q,&item); printf("%d\n",item); DeQ(q,&item); printf("%d\n",item); DeQ(q,&item); printf("%d\n",item); DeQ(q,&item); printf("%d\n",item); destroy(&q); return 0;} </span>
0 0
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 饿了么移动APP的架构演进
- CDH5.5.1 安装指南 中文首发版
- Ruby对象、变量和常量
- javaEE之------添加JavaScript中自己写的函数
- RoundedImageView作为圆形imageview的使用记录
- 队列
- Android 中 单元测试的配置
- UISearchDisplayController点击搜索时屏幕顶部出现黑条问题的解决方案
- C++学习笔记
- 红米2移动稳定版 刷开发版本
- 06day 分割编译与中断处理
- AlexNet论文翻译
- python初学selenium+phantomjs遇到的问题
- Eclipse RCP 插件开发中经常调用的几个方法