#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 typedef int QElemtype; typedef int status;
typedef struct QNode //对节点的结构定义
{
QElemtype data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{ //对队列的结构定义
QueuePtr head;
QueuePtr rear;
}LinkQueue;
status initQueue(LinkQueue* que) //初始化队列
{
que->head=que->rear=(QueuePtr)malloc(sizeof(QNode));
if(!que->head) //这段代码对队列里面的用户自定义数据类型进行了初始化
return ERROR;
return OK;
}
status destoryQueue(LinkQueue* que) //回收队列
{
while(que->head)
{
que->rear = que->head->next;
free(que->head);
que->head=que->rear;
}
return OK;
}
循环条件是 队列的队头节点 head 存在
{
此处rear起到临时变量的作用,不断指向head->next的同时,释放head。这样处理可以干净的把包含头节点在内的队列清理干净。
}
status enQueue(LinkQueue* que,QElemtype e)
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p) //若未能申请到空间,便退出
return ERROR;
p->data=e;
p->next=NULL;
que->rear->next = p;
que->rear=p;
return OK;
}
status delQueue(LinkQueue* que,QElemtype *t)
{
if(que->rear==que->head)
return ERROR; //队列为空
QueuePtr p = que->head->next;
*t=p->data;
que->head->next=p->next;
if(que->rear==p) //这个判断是 确保在清空队列的时候,让rear指针归位。
que->rear=que->head;
free(p);
return OK;
}
status viewQueue(LinkQueue* que)
{
if(que->rear == que->head)
return ERROR;
QueuePtr p =que->head->next;
while(p)
{
printf("val:%d",p->data);
p=p->next;
}
return OK;
}
int main(int argc, char **argv)
{
LinkQueue myQueue;
initQueue(&myQueue);
for(int i=1;i<=5;i++)
enQueue(&myQueue,i);
viewQueue(&myQueue);
QElemtype a;
for(int i=0;i<5;i++)
{
delQueue(&myQueue,&a);
printf("%d\n",a);
}
destoryQueue(&myQueue);
printf("fuck !");
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有