数据结构-链队列
来源:互联网 发布:java的静态代理 编辑:程序博客网 时间:2024/06/06 01:34
供学习之用,参照他人理解学习
/*队列先进先出,实际上创建了一个链表,由头指针,尾指针分别指向链表的头和尾,创建完成之后尾指针指向最后,头指针和尾指针操作同“一根”链(表)1先进队列,也是出对列第一个1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 3 4 5 6 7 8 9 4 5 6 7 8 9 5 6 7 8 9 6 7 8 9 7 8 98 9 9*/#include <stdlib.h>#include <string.h>#include <stdio.h>#define QElemType int#define OVERFLOW -1typedef struct QueueNode{QElemType data;struct QueueNode* pNext;}QueueNode;typedef struct LinkQueue{struct QueueNode* front;/*队列头指针*/struct QueueNode* rear;/*队列尾指针*/}LinkQueue;void Init_queue(LinkQueue* pQueue) // the first node is always NULL,recommend the next style{pQueue->rear = pQueue->front = (QueueNode*)malloc(sizeof(QueueNode));if(pQueue->front == NULL){printf("apply for memory failed!\n");exit(0);}pQueue->rear->pNext = NULL;}void InitQueue(LinkQueue* pQueue){pQueue->front = pQueue->rear = NULL;}void AddQueue(LinkQueue* pQueue,QElemType data){QueueNode* ptr = (QueueNode*)malloc(sizeof(QueueNode));ptr->data = data;ptr->pNext = NULL;if(pQueue->front == NULL)pQueue->front = pQueue->rear = ptr;else{pQueue->rear->pNext = ptr;pQueue->rear = ptr;}}void Dequeue(LinkQueue* pQueue){if(pQueue->front == NULL){printf("the queue is empty!\n");exit(0);}printf("dequeue data %d\n",pQueue->front->data);pQueue->front = pQueue->front->pNext;}void DestroyQueue(LinkQueue* pQueue) {QueueNode* p = pQueue->front;while(!p){pQueue->front = pQueue->rear;free(p);p = pQueue->front;}pQueue->rear = NULL;//free the last node}void ShowQueue(LinkQueue* pQueue){QueueNode* p = pQueue->front;while(p!= NULL){printf("data %d \n",p->data);p = p->pNext;}printf("\n");}QElemType LengthQueue(LinkQueue* pQueue){int length = 0;QueueNode* p = pQueue->front;while(p != NULL){length++;p = p->pNext;}return length;}int main(){int i = 0;LinkQueue pQueue;InitQueue(&pQueue);for(i = 0; i < 10; i++)AddQueue(&pQueue,i);ShowQueue(&pQueue); for(i = 0; i < 10; i++){Dequeue(&pQueue);printf("Now,the length is %d\n",LengthQueue(&pQueue));ShowQueue(&pQueue);}system("pause");return 0;}
0 0
- 数据结构之链队列
- 数据结构链队列算法
- 数据结构链表队列
- 数据结构-链表队列
- 数据结构----链表队列
- 【数据结构】链队列_LinkQueue
- 数据结构之链队列
- 数据结构之链队列
- 数据结构之链队列
- 数据结构-链队列
- 数据结构之链队列
- 数据结构之链队列
- 数据结构_链队列
- 数据结构6链队列
- 链队列-数据结构(11)
- 数据结构:链表队列
- 数据结构之链队列
- 数据结构--链队列
- postgre常用系统函数
- 14. Linux下的用户管理
- 小黑小波比.随机班号
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(下)
- Zookeeper写文件原子类
- 数据结构-链队列
- 蓝桥杯备战-阶乘计算
- 程序开发的一些思考
- js取年月最后一天
- Android抽象布局——include、merge 、ViewStub
- 删除项目中文件夹下包含的 .svn .git CVS等等遗留文件
- AnnotationAwareAspectJAutoProxyCreator is only available on Java 1.5 and higher
- PP(1) 基本流程
- 关于普朗克的量子化假说一些故事