C语言实现链队列代码
来源:互联网 发布:条件选股软件 编辑:程序博客网 时间:2024/06/05 08:13
#include <stdio.h>/* 队列的结构体 */typedef int DataType;#define NODE_LEN sizeof(NODE) /* 队列的节点 */typedef struct stNode{ DataType data; struct stNode* next;}NODE;/* 队列 */typedef struct stQueue{ NODE* head; //队列的头 NODE* tail; //队列的尾}QUEUE;/* 初始化队列,不带头结点*/int initQueue(QUEUE* INQueue){ INQueue->head = NULL; INQueue->tail = NULL; return 0;}/* 从队尾插入一个元素 */int enQueue(QUEUE* InQueue,DataType InData){ NODE* pNewNode = (NODE*)malloc(NODE_LEN); if (pNewNode == NULL) { return -1; } pNewNode->data = InData; pNewNode->next = NULL; /* 判断,现在队列里面有没有节点 */ if (InQueue->head == NULL) { InQueue->head = pNewNode; InQueue->tail = pNewNode; } else { InQueue->tail->next = pNewNode; InQueue->tail = pNewNode; } return 0;}/* 遍历该队列 */int visitQueue(QUEUE InQueue){ QUEUE* pstTemp = &InQueue; /* 判断队列是否为空队列 */ if (pstTemp->head == NULL) { printf("visitQueue: this queue is empty\n"); return -1; } /* 遍历该队列中的所有元素 */ while (pstTemp->head->next != NULL) { printf("%d ", pstTemp->head->data); pstTemp->head = pstTemp->head->next; } printf("%d \n", pstTemp->head->data); return 0;}/* 出队列 */int delQueue(QUEUE* InQueue,DataType* OutData){ if (InQueue->head == NULL) { printf("delQueue: this queue is empty\n"); return -1; } *OutData = InQueue->head->data; NODE* pstTemp = InQueue->head; InQueue->head = InQueue->head->next; delete pstTemp; return 0;}/* 判断队列是否是空队列 */int isEmptyQueue(QUEUE InQueue){ if (InQueue.head == NULL) { return 0; //是空队列 } return 1; //不是空队列}int main(){ /* 创建一个队列 */ QUEUE queue; DataType data; initQueue(&queue); /* 入队列 */ enQueue(&queue, 12); enQueue(&queue, 11); enQueue(&queue, 2); visitQueue(queue); /* 出队列 */ delQueue(&queue, &data); visitQueue(queue); printf("data = %d\n", data); visitQueue(queue); if (0 == isEmptyQueue(queue)) { printf("This is empty queue\n"); } else { printf("This is not empty queue\n"); } return 0;}
0 0
- C语言实现链队列代码
- C语言实现链队列
- 队列 C语言实现
- 队列C语言实现
- C语言实现队列
- 队列 c语言实现
- C语言实现队列
- C语言实现队列
- C 语言实现队列
- c语言实现队列
- C语言实现队列
- C语言队列实现
- C语言实现队列
- C语言二叉树与队列实现基础代码
- 数据结构中栈与队列的c语言代码实现
- C语言实现数据结构中的链队列
- 链队列的实现 C语言
- 链队列的C语言实现
- Hello World♂
- 开发前奏—熟悉业务
- 阿里推荐大赛:ODPS SQL 构建离线评估
- 线程私有数据TSD——一键多值技术,线程同步中的互斥锁和条件变量
- openssl dgst命令使用示例
- C语言实现链队列代码
- 进程与线程
- 进程间通信之管道和FIFO
- Java虚拟机类加载和执行机制
- C# 遍历文件夹及其子文件夹来查找某个文件
- scala学习之for与function的认识
- python中的zlib模块提供了压缩和解压缩的方法
- android 何时使用Service 何时使用Thread
- 第76讲:模式匹配下的赋值语句