数据结构-循环队列的基本实现操作

来源:互联网 发布:农村淘宝下载官网 编辑:程序博客网 时间:2024/05/30 02:24
/*main*/#include<stdio.h>#include<stdlib.h>#define TRUE1#define ERROR0#define FALSE0#define OK1#define OVERFLOW-2#defineMAXSIZE20typedef int Status;typedef int QElemtype;typedef struct{QElemtype*base;int front;int rear;}SqQueue;#include"xhdl.h"main(){SqQueue Q;Init(Q);int cnt=1,k;while(cnt<11){EnQueue(Q,cnt);cnt++;} printf("循环队列中元素:");print(Q);putchar(10);printf("队列此时长度:%d",QueueLength(Q));putchar(10);printf("出队元素:");for(int i=0;i<2;i++)printf("%d ",DeQueue(Q,k));putchar(10);printf("循环队列中元素:");print(Q);putchar(10);printf("队列此时长度:%d",QueueLength(Q));return OK;}/*xhdl*/StatusInit(SqQueue &Q){Q.base=(int *)malloc(MAXSIZE*sizeof(int));if(!Q.base)exit(OVERFLOW);Q.front=Q.rear=0;return OK; }Status EnQueue(SqQueue &Q,QElemtype e){if((Q.rear+1)%MAXSIZE==Q.front)return ERROR;//队列满的判定条件; Q.base[Q.rear]=e;Q.rear=(Q.rear+1)%MAXSIZE;return OK; }Status QueueLength(SqQueue Q){return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;}Status DeQueue(SqQueue &Q,int &e){if(Q.front==Q.rear)return ERROR;//队列空的判定条件;e=Q.base[Q.front];Q.front=(Q.front+1)%MAXSIZE;return e; }Status print(SqQueue &Q){if(Q.front==Q.rear){printf("队列为空"); return ERROR;}int i=Q.front;if(Q.front<Q.rear){while(i<Q.rear){printf("%d ",Q.base[i]);i++;}}else{while(i<=MAXSIZE){printf("%d ",Q.base[i]);i++;}int j=0;while(j<Q.rear) {printf("%d ",Q.base[j]);j++;}}}

0 0
原创粉丝点击