赞
踩
- #include<stdio.h>
- #include<stdlib.h>
- #define MAXQSIZE 100 //队列可能达到的最大长度
- typedef int QElemType;
- typedef int Status;
- #define OK 1
- #define OVERFLOW 0
- #define ERROR 0
- typedef struct
- {
- QElemType *base;//存储空间的基地址;
- int front;//头指针
- int raer;//尾指针
- }SqQueue;//循环队列;
①为队列分配一个容量为MAXQSIZE的数组空间,base指向首地址;
②将头指针和尾指针置为0,表队空;
- Status InitQueue(SqQueue &Q)
- {//构造一个空队列Q;
- Q.base=new QElemType[MAXQSIZE];//为队列分配一个最大容量位MAXQSIZE的数组空间;
- if(!Q.base) exit(OVERFLOW);//存储分配失败
- Q.front=Q.raer=0;//将头指针和尾指针置为0,队列为空;
- return OK;
- }
①将头指针减去尾指针加上最大数组空间然后进行取余
- void QueueLength(SqQueue Q)
- {//返回Q的元素个数,即队列的长度;
- int x=(Q.raer-Q.front+MAXQSIZE)%MAXQSIZE;
- printf("队列长度为:%d\n",x);
- }
①判断队列是否满,若满则返回ERROR;
②将新元素插入队尾;
③队尾元素+1;
- Status EnQueue(SqQueue &Q,QElemType e)
- {//插入元素为e位Q的新的队尾元素
- if((!Q.raer+1)%MAXQSIZE==Q.front)
- //若尾指针在循环意义上加1后等于头指针,表明队满
- return ERROR;
- Q.base[Q.raer]=e;//新元素插入队尾
- Q.raer=(Q.raer+1)%MAXQSIZE;//队尾指针加1
- return OK;
- }
①判断队列是否为空,若空则返回ERROR;
②保存队头元素;
③队头指针+1;
- Status DeQueue(SqQueue &Q,QElemType &e)
- {//删除Q的队头元素,用 e返回其值
- if(Q.front==Q.raer) return ERROR;//队空 ;
- e=Q.base[Q.front];//保存队头元素 ;
- printf("将元素%d出队\n",e);
- Q.front=(Q.front+1)%MAXQSIZE;//队头指针加1;
- return OK;
- }
①Q.base为队头序号,直接利用Q.base输出;
- QElemType GetHead(SqQueue Q)
- {//返回Q的队头元素,不修改队头指针
- printf("队头元素为:");
- if(Q.front!=Q.raer) //队列非空
- return Q.base[Q.front];//返回
- }
- int main()
- {
- SqQueue Q;
- int x,e;
- InitQueue(Q);
- EnQueue(Q,0);
- EnQueue(Q,1);
- EnQueue(Q,2);
- EnQueue(Q,3);
- QueueLength(Q);
- DeQueue(Q,e);
- DeQueue(Q,e);
- QueueLength(Q);
- int k=GetHead(Q) ;
- printf("%d",k);
- return 0;
-
- }

- #include<stdio.h>
- #include<stdlib.h>
- #define MAXQSIZE 100 //队列可能达到的最大长度
- typedef int QElemType;
- typedef int Status;
- #define OK 1
- #define OVERFLOW 0
- #define ERROR 0
- typedef struct
- {
- QElemType *base;//存储空间的基地址;
- int front;//头指针
- int raer;//尾指针
- }SqQueue;//循环队列;
- Status InitQueue(SqQueue &Q)
- {//构造一个空队列Q;
- Q.base=new QElemType[MAXQSIZE];//为队列分配一个最大容量位MAXQSIZE的数组空间;
- if(!Q.base) exit(OVERFLOW);//存储分配失败
- Q.front=Q.raer=0;//将头指针和尾指针置为0,队列为空;
- return OK;
- }
- void QueueLength(SqQueue Q)
- {//返回Q的元素个数,即队列的长度;
- int x=(Q.raer-Q.front+MAXQSIZE)%MAXQSIZE;
- printf("队列长度为:%d\n",x);
- }
- Status EnQueue(SqQueue &Q,QElemType e)
- {//插入元素为e位Q的新的队尾元素
- if((!Q.raer+1)%MAXQSIZE==Q.front)
- //若尾指针在循环意义上加1后等于头指针,表明队满
- return ERROR;
- Q.base[Q.raer]=e;//新元素插入队尾
- Q.raer=(Q.raer+1)%MAXQSIZE;//队尾指针加1
- return OK;
- }
- Status DeQueue(SqQueue &Q,QElemType &e)
- {//删除Q的队头元素,用 e返回其值
- if(Q.front==Q.raer) return ERROR;//队空 ;
- e=Q.base[Q.front];//保存队头元素 ;
- printf("将元素%d出队\n",e);
- Q.front=(Q.front+1)%MAXQSIZE;//队头指针加1;
- return OK;
- }
- QElemType GetHead(SqQueue Q)
- {//返回Q的队头元素,不修改队头指针
- printf("队头元素为:");
- if(Q.front!=Q.raer) //队列非空
- return Q.base[Q.front];//返回
- }
- int main()
- {
- SqQueue Q;
- int x,e;
- InitQueue(Q);
- EnQueue(Q,0);
- EnQueue(Q,1);
- EnQueue(Q,2);
- EnQueue(Q,3);
- QueueLength(Q);
- DeQueue(Q,e);
- DeQueue(Q,e);
- QueueLength(Q);
- int k=GetHead(Q) ;
- printf("%d",k);
- return 0;
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。