C语言实现链式队列

来源:互联网 发布:php中变量的作用域 编辑:程序博客网 时间:2024/05/17 02:28

今天庆祝抗战70周年,而后很快就要开学了,好好享受开学前的闲暇时光吧奋斗有空会回去poj玩玩的

#include<stdio.h>#include<stdlib.h>#define datatype inttypedef struct queue {datatype data;struct queue *pNext;} Queue,*PQueue;//初始化void init(PQueue *pphead) {*pphead=NULL;}//入队PQueue enq(PQueue phead,datatype pdata) {PQueue pnew=(PQueue)malloc(sizeof(Queue));pnew->data=pdata;pnew->pNext=NULL;if(phead==NULL) {phead=pnew;} else {PQueue p=phead;while(p->pNext!=NULL) {p=p->pNext;}p->pNext=pnew;//尾部插入}return phead;}//出队PQueue deq(PQueue phead,datatype *pdata) {if(phead==NULL) {return NULL;} else {*pdata=phead->data;PQueue p=phead;phead=phead->pNext;free(p);}return phead;}//显示队列所有数据void show(PQueue phead) {if(phead==NULL) {return;} else {printf("data=%d,phead=%p,pNext=%p\n",phead->data,phead,phead->pNext);show(phead->pNext);}}main() {PQueue phead;init(&phead);for(int i=1; i<=10; i++) {phead=enq(phead,i);//printf("\n--------------------------------\n");//show(phead);}while(phead!=NULL) {datatype data;phead=deq(phead,&data);printf("%d\n",data);}}
                                        

0 0