structComm.h //定义各种结构的头文件

#ifndefSTRUCTCOMM_H_INCLUDED#defineSTRUCTCOMM_H_INCLUDED#undefNULL#ifdef__cplusplus#defineNULL0#else#defineNULL((void*)0)#endifstructlist_head{structlist_head*next;structlist_head*prev;};typedefstructtag_myTree{intdata;structtag_myTree*pLeft;structtag_myTree*pRight;}myTree;typedefstructtag_myStack{intdata;myTree*pTree;structlist_headstStack;}myStack;typedefstructtag_myQue{intdata;myTree*pTree;structlist_headstQue;}myQue;#endif//STRUCTCOMM_H_INCLUDED

myQue.h

#ifndefMYQUE_H_INCLUDED#defineMYQUE_H_INCLUDED#include"structComm.h"myQue*getNewNode();voidinitQue(myQue*pRoot);voiddestoryQue(myQue*pRoot);intgetQueLen(myQue*pRoot);intisQueEmpty(myQue*pRoot);voidenQue(myQue*pRoot,myQue*pNew);myQue*deQue(myQue*pRoot);#endif//MYQUE_H_INCLUDED

myQue.c

#include"myQue.h"#include"myList.h"myQue*getNewQueNode(){myQue*pTmp=NULL;pTmp=(myQue*)malloc(sizeof(myQue));if(NULL==pTmp){returnNULL;}pTmp->data=0;pTmp->pTree=NULL;INIT_LIST_HEAD(&(pTmp->stQue));returnpTmp;}voidinitQue(myQue*pRoot){pRoot->data=0;pRoot->pTree=NULL;INIT_LIST_HEAD(&(pRoot->stQue));return;}voiddestoryQue(myQue*pRoot){structlist_head*pos=NULL;structlist_head*n=NULL;myQue*pstQue=NULL;list_for_each_safe(pos,n,&(pRoot->stQue)){list_del(pos);pstQue=list_entry(pos,myQue,stQue);free(pstQue);pos=n;}return;}intgetQueLen(myQue*pRoot){intlen=0;structlist_head*pos=NULL;list_for_each(pos,&(pRoot->stQue)){len++;}returnlen;}intisQueEmpty(myQue*pRoot){returnlist_empty(&(pRoot->stQue));}voidenQue(myQue*pRoot,myQue*pNew){list_add(&(pNew->stQue),&(pRoot->stQue));}myQue*deQue(myQue*pRoot){myQue*pstQue=NULL;structlist_head*pTmp=NULL;if(!isQueEmpty(pRoot)){pTmp=(pRoot->stQue).prev;list_del((pRoot->stQue).prev);pstQue=list_entry(pTmp,myQue,stQue);returnpstQue;}returnNULL;}

main.c

#include<stdio.h>#include"myQue.h"myQueg_stQueRoot;//栈的头节点,不存储数据intmain(){inti=0;myQue*pNewNode=NULL;initQue(&g_stQueRoot);//初始化之前要保证头队列为空printf("isempty:\t%d\n",isQueEmpty(&g_stQueRoot));for(i=0;i<5;i++){pNewNode=getNewNode();if(NULL==pNewNode){exit(0);}pNewNode->data=i;enQue(&g_stQueRoot,pNewNode);}printf("isempty:\t%d\n",isQueEmpty(&g_stQueRoot));printf("quelen:\t%d\n",getQueLen(&g_stQueRoot));printf("de:\t");for(i=0;i<5;i++){pNewNode=deQue(&g_stQueRoot);printf("%d\t",pNewNode->data);}pNewNode=deQue(&g_stQueRoot);if(NULL==pNewNode){printf("\ncannotde\n");}return0;}