队列,数组式

来源:互联网 发布:python爬虫教程 pdf 编辑:程序博客网 时间:2024/06/05 19:05

就当作学习数据结构的记忆吧

//队列,数组式#include <stdio.h>#include <stdlib.h>#define ELEMTYPE int#define STATUS int#define STRUCTSIZE sizeof(QUEUE)#define ELEMSIZE sizeof(ELEMTYPE)typedef struct QUEUE{ELEMTYPE *epData;int iFront;int iRear;int iQueueSize;//循环队列存在闲置节点,该值不包含闲置节点}QUEUE,*QUEUEP;QUEUEP queueInit(int iSize);STATUS isFull(QUEUEP spQueue);STATUS isEmpty(QUEUEP spQueue);STATUS queueInput(QUEUEP spQueue,ELEMTYPE eTemp);STATUS queueOutput(QUEUEP spQueue,ELEMTYPE *eTemp);ELEMTYPE queueGetFirst(QUEUEP spQueue);void queueFree(QUEUEP *sppQueue);void main(){QUEUEP spQueue=NULL;int iTemp=0;int iTemp2=0;spQueue=queueInit(10);while(1)if(queueInput(spQueue,iTemp++)==-1)break;printf("%d\n",queueGetFirst(spQueue));while(iTemp2<5)if(queueOutput(spQueue,&iTemp)==1){printf("%d ",iTemp);iTemp2++;}elsebreak;printf("\n");printf("%d\n",queueGetFirst(spQueue));while(1)if(queueInput(spQueue,iTemp2++)==-1)break;while(1)if(queueOutput(spQueue,&iTemp)==1)printf("%d ",iTemp);elsebreak;printf("\n");queueFree(&spQueue);}QUEUEP queueInit(int iSize)//iSize实际节点数{QUEUEP spTemp=NULL;if(iSize<=0)return NULL;spTemp=malloc(STRUCTSIZE);if(!spTemp)return NULL;(*spTemp).epData=malloc(ELEMSIZE*(iSize+1));if(!(*spTemp).epData)return NULL;(*spTemp).iFront=(*spTemp).iRear=0;(*spTemp).iQueueSize=iSize;return spTemp;}STATUS isFull(QUEUEP spQueue)//队满返回1{if( ((*spQueue).iRear+1) % ((*spQueue).iQueueSize+1) == (*spQueue).iFront )return 1;return -1;}STATUS isEmpty(QUEUEP spQueue)//队空返回1{if((*spQueue).iRear==(*spQueue).iFront)return 1;return -1;}STATUS queueInput(QUEUEP spQueue,ELEMTYPE eTemp)//成功返回1{if(isFull(spQueue)==1)return -1;(*spQueue).epData[(*spQueue).iRear]=eTemp;(*spQueue).iRear=((*spQueue).iRear+1)%((*spQueue).iQueueSize+1);return 1;}STATUS queueOutput(QUEUEP spQueue,ELEMTYPE *epTemp)//成功返回1//注意形参{if(isEmpty(spQueue)==1)return -1;*epTemp=(*spQueue).epData[(*spQueue).iFront];(*spQueue).iFront=((*spQueue).iFront+1)%((*spQueue).iQueueSize+1);return 1;}ELEMTYPE queueGetFirst(QUEUEP spQueue)//数据域的类型改变,此函数返回值亦须改变{if(isEmpty(spQueue)==1)return -1;return (*spQueue).epData[(*spQueue).iFront];}void queueFree(QUEUEP *sppQueue)//注意形参{if(!(*sppQueue))return;if((*(*sppQueue)).epData)free((*(*sppQueue)).epData);free(*sppQueue);*sppQueue=NULL;return;}


0 0
原创粉丝点击