文档库 最新最全的文档下载
当前位置:文档库 › 数据结构C语言版实验五 循环队列的基本操作

数据结构C语言版实验五 循环队列的基本操作

实验五 循环队列的基本运算
1、实验目的
掌握循环队列的基本操作,初始化、求队列长度、入队、出队和显示等运算在顺序存储结构上的实现。
2、实验内容
(1)循环队列的初始化;
(2)循环队列入队操作的实现;
(3)循环队列出队操作的实现;
(4)显示循环队列中各个元素;
(5)求循环队列的长度。
3、实验要求
(1)能够熟练在Visual C++6.0环境中进行程序的编辑、编译和调试;
(2)会书写类C语言的算法,并将算法转变为程序实现。
4、程序运行框架
#include
#include

#define MAXQSIZE 5
typedef char QElemType;
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;

int InitQueue(SqQueue &Q) {return 0; }
int QueueLength(SqQueue Q) {return 0; }
int EnQueue(SqQueue &Q,QElemType e) {return 0; }
int DeQueue(SqQueue &Q,QElemType &e) {return 0; }
void DispQueue(SqQueue Q){ }
void main(){}

5、测试数据:
补充程序,在循环队列中依次将元素A、B、C、D入队,求队列长度之后出队,再将E、F入队最后显示队列元素,并观察运行结果。

实验内容可执行程序:
#include
#include
#define MAXQSIZE 5
typedef char QElemType;
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;

int InitQueue(SqQueue &Q) {
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(SqQueue));
if(!Q.base) return 0;
Q.front=Q.rear=0;
return 1;
}

int QueueLength(SqQueue Q) {
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

int EnQueue(SqQueue &Q,QElemType e) {
if((Q.rear+1)% MAXQSIZE ==Q.front) return 0;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return 1;
}
int DeQueue(SqQueue &Q,QElemType &e) {
if(Q.front==Q.rear) return 0;
e=Q.base[Q.front];
Q.front=(Q.front+1)% MAXQSIZE;
return 1;
}

void DispQueue(SqQueue Q){
int i,j;
j=QueueLength(Q);
if(j==0) printf("该队列为空队列!!\n");
for(i=1;i<=j;i++){
printf("%c",Q.base[Q.front]);
Q.front=(Q.front+1)% MAXQSIZE;
}
}

void main(){
int k;
QElemType e;
SqQueue Q;
InitQueue(Q);
DispQueue(Q);
EnQueue(Q,'A');
EnQueue(Q,'B');
EnQueue(Q,'C');
EnQueue(Q,'D');
printf("对列为:");
DispQueue(Q);
printf("\n");
printf("长度为:");
k=QueueLength(Q);
printf("%d",k);
printf("\n");
DeQueue(Q,e);
DeQueue(Q,e);
DeQueue(Q,e);
DeQueue(Q,e);
DispQueue(Q);
EnQueue(Q,'E');
EnQueue(Q,'F');
printf("对列为:");
DispQueue(Q);
printf("\n");
}

相关文档
相关文档 最新文档