C语言实现链队列代码
2015-08-04 22:41
549 查看
[code]#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; }
相关文章推荐
- 10、C语言和设计模式(迭代器模式)
- C语言的基本类型
- 9、C语言和设计模式(抽象工厂模式)
- 8、C语言和设计模式(责任链模式)
- 黑马程序员 c语言 条件判断
- C语言函数的调用
- C语言:链表的创建、增、删、查
- 7、C语言和设计模式(工厂模式)
- 《高质量程序设计指南C/C++语言》 林锐 韩永泉编著 总结3
- 什么是c++中的多态
- 6、C语言和设计模式(之模板模式)
- C++学习
- [C++学习笔记]--常量对象、常量成员函数和常引用
- [C++学习笔记]--友元、this指针
- C++ explicit 关键字
- 《高质量程序设计指南C/C++》 林锐 韩永泉编著 总结2
- C语言及程序设计进阶例程-39 银行储蓄系统(第六版)开发
- C++语言中的四种类型转换
- C++语言中的四种类型转换
- hdoj2191多重背包化为01背包