您的位置:首页 > 其它

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

2014-07-01 15:42 666 查看
參考书目:《数据结构(C语言版)》,严蔚敏

怎样将《数据结构》中的知识应用到如今的工作中呢(单片机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    -2

typedef int Status;

//Status Equal(int a, int b);

#endif


SqQueue.h

#ifndef _SQQUEUE_H_
#define _SQQUEUE_H_

#define MAXQSIZE	10

typedef 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,否则返回ERROR

Status 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);
}
//===================================================================
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: