C语言实现链队列代码
本文实例为大家分享了C语言实现链队列的具体代码,供大家参考,具体内容如下
#include/*队列的结构体*/ typedefintDataType; #defineNODE_LENsizeof(NODE) /*队列的节点*/ typedefstructstNode { DataTypedata; structstNode*next; }NODE; /*队列*/ typedefstructstQueue { NODE*head;//队列的头 NODE*tail;//队列的尾 }QUEUE; /*初始化队列,不带头结点*/ intinitQueue(QUEUE*INQueue) { INQueue->head=NULL; INQueue->tail=NULL; return0; } /*从队尾插入一个元素*/ intenQueue(QUEUE*InQueue,DataTypeInData) { 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; } return0; } /*遍历该队列*/ intvisitQueue(QUEUEInQueue) { QUEUE*pstTemp=&InQueue; /*判断队列是否为空队列*/ if(pstTemp->head==NULL) { printf("visitQueue:thisqueueisempty\n"); return-1; } /*遍历该队列中的所有元素*/ while(pstTemp->head->next!=NULL) { printf("%d",pstTemp->head->data); pstTemp->head=pstTemp->head->next; } printf("%d\n",pstTemp->head->data); return0; } /*出队列*/ intdelQueue(QUEUE*InQueue,DataType*OutData) { if(InQueue->head==NULL) { printf("delQueue:thisqueueisempty\n"); return-1; } *OutData=InQueue->head->data; NODE*pstTemp=InQueue->head; InQueue->head=InQueue->head->next; deletepstTemp; return0; } /*判断队列是否是空队列*/ intisEmptyQueue(QUEUEInQueue) { if(InQueue.head==NULL) { return0;//是空队列 } return1;//不是空队列 } intmain() { /*创建一个队列*/ QUEUEqueue; DataTypedata; 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("Thisisemptyqueue\n"); } else { printf("Thisisnotemptyqueue\n"); } return0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短