循环队列——队列的顺序表示和实现

来源:互联网 发布:淘宝网广西金源马蹄粉 编辑:程序博客网 时间:2024/05/24 03:59

参考书目:《数据结构(C语言版)》,严蔚敏

        本例程用循环队列实现了一个简单的应用模型(得益于一位童鞋的启发)。

common.h

#ifndef_COMMON_H_#define_COMMON_H_#define TRUE        1#define FALSE       0#define OK          1#define ERROR       0#define INFEASIBLE  -1#define OVERFLOW    -2typedef int Status;  //Status Equal(int a, int b);#endif


SqQueue.h

#ifndef _SQQUEUE_H_#define _SQQUEUE_H_#define MAXQSIZE10typedef int QElemType;typedef struct{QElemType *base;int      front;int  rear;}SqQueue;Status InitQueue(SqQueue *Q);//构造一个空队列int QueueLength(SqQueue *Q);//返回队列*Q的元素个数,即队列的长度Status EnQueue(SqQueue *Q,QElemType e);//插入元素e为Q的新的队尾元素Status DeQueue(SqQueue *Q,QElemType *e);//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERRORStatus QueueDisp(SqQueue *Q);//显示整个队列Status QueueScanf(SqQueue *Q);//向指定的队列输入数据,约定当输入0时结束,并不将0存入队列Status QueueScanfApp(SqQueue *Q);//调用EnQueue()函数向指定队列中输入数据,当队列满 或 输入0 时结束#endif


SqQueue.c

#include "common.h"#include "SqQueue.h"#include "stdio.h"#include "stdlib.h"/*函数功能:构造一个空队列*/Status InitQueue(SqQueue *Q){Q->base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));if(!Q->base)exit(OVERFLOW);Q->front = Q->rear = 0;return OK;}/*函数功能:返回队列*Q的元素个数,即队列的长度*/int QueueLength(SqQueue *Q){return((Q->rear - Q->front + MAXQSIZE)%MAXQSIZE);}/*输入参数:*Q待插入的队列e待插入的元素函数功能:插入元素e为Q的新的队尾元素*/Status EnQueue(SqQueue *Q,QElemType e){if((Q->rear + 1)%MAXQSIZE == Q->front)return OVERFLOW;Q->base[Q->rear] = e;Q->rear = (Q->rear + 1) % MAXQSIZE;return OK;}/*输入参数:*Q待处理的队列   *e用于存储删除的元素函数功能:若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR*/Status DeQueue(SqQueue *Q,QElemType *e){if(Q->front == Q->rear)return ERROR;*e = Q->base[Q->front];Q->front = (Q->front + 1) % MAXQSIZE;//这里对MAXQSIZE取余还有啥意义呢?return OK;}/*函数功能:显示整个队列*/Status QueueDisp(SqQueue *Q){int head = Q->front;printf("\n>>>>>>>>>>>>>>> output start >>>>>>>>>>>>>>>\n");while(head - Q->rear){printf("%d  ",Q->base[head]);head++;}printf("\n<<<<<<<<<<<<<<< output end <<<<<<<<<<<<<<<\n");return OK;}/*函数功能:向指定的队列输入数据,约定当输入0时结束,并不将0存入队列*/Status QueueScanf(SqQueue *Q){QElemType temp = 0;if((Q->rear + 1)%MAXQSIZE == Q->front)return OVERFLOW;scanf("%d",&temp);while(temp){Q->base[Q->rear] = temp;Q->rear++;if((Q->rear + 1)%MAXQSIZE == Q->front)//每次都要判断一下 其实挺费时间的,实际应用时应另想对策。return OVERFLOW;scanf("%d",&temp);}return OK;}/*函数功能:调用EnQueue()函数向指定队列中输入数据,当队列满 或 输入0 时结束说明:QueueScanf()这个函数只是平时学习的时候玩的一个东东,而真正队列的输入和输出(插入和删除)操作应该是用EnQueue()和DeQueue()函数来实现的。即类似这样的函数。*/Status QueueScanfApp(SqQueue *Q){QElemType temp = 0;int overflow_flag=0;scanf("%d",&temp);while(temp && overflow_flag!=OVERFLOW){overflow_flag = EnQueue(Q,temp);scanf("%d",&temp);}return OK;}


main.c

#include "common.h"#include "SqQueue.h"#include "stdio.h"#include "stdlib.h"int main(){QElemType Qhead=0;SqQueue queue;InitQueue(&queue);//===================================================================printf("Please input data for queue:\n");QueueScanf(&queue);printf("The current queue length is %d\n.",QueueLength(&queue));printf("All elements of the queue is:\n");QueueDisp(&queue);EnQueue(&queue,1314);EnQueue(&queue,520);printf("All elements after insert datas.\n");QueueDisp(&queue);printf("The current queue length is %d\n",QueueLength(&queue));DeQueue(&queue,&Qhead);printf("The deleted data is:%d\n",Qhead);printf("All elements of the queue is:\n");QueueDisp(&queue);printf("The current queue length is %d\n",QueueLength(&queue));printf("\n\n");//===================================================================//下面这段代码应该是实际队列应用的一个缩影。//===================================================================/*这段程序的功能是:step1:先输出当前队列的长度,长度为0时表示当前队列空;step2:判断循环队列中是否有数,若有则输出全部的队列元素,并将队列清空 等待下次输入数据的到来;step3:输入数据,当队列满时结束,并在此基础上约定当输入0时也结束;step4:返回执行step1,如此往复。注意:当然也可以约定当输入某个数字或字符(如:end)就结束整个程序。如果逐个判断的话效率很低,但用软中断应该可以。或者还有什么其他的方法吗?*/while(1){printf("The current queue length is %d.\n",QueueLength(&queue));printf("~~~~~~~~~~~~~~ start ~~~~~~~~~~~~~~\n");while(QueueLength(&queue)){DeQueue(&queue,&Qhead);printf("%d  ",Qhead);}printf("\n~~~~~~~~~~~~~~ end ~~~~~~~~~~~~~~\n");//getch();QueueScanfApp(&queue);}//===================================================================}

 

1 0
原创粉丝点击