循环队列的实验报告.doc
文本预览下载声明
数据结构实验报告
实验名称:循环队列的的练习操作 评分: 专业: 信管财会 班级: 一班 小组编号:三组
实验内容:
采用顺序存储实现循环队列的初始化、入队、出队等操作。
实验目的:
熟练掌握循序队列的相关操作
实验源程序:(源程序必须调试通过;需有必要的注释;源程序可打印,附在本实验报告之后。)
#include stdio.h
#include stdlib.h
#define ERROR 0
#define OK 1
#define OVERFLOW -2
#define MAXQSIZE 100 //最大队列长度
typedef int QElemType;
typedef int Status;
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue (SqQueue Q){
//构造一个空队列Q
Q.base = (QElemType *) malloc (MAXQSIZE * sizeof(QElemType));
if(!Q.base ) exit (OVERFLOW); //存储分配失败
Q.front = Q.rear = 0;
return OK;
}
int QueueLength (SqQueue Q){
//返回Q的元素个数,及对列的长度
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
Status EnQueue (SqQueue Q,QElemType e){
//插入元素e为Q的新的队尾元素
if ((Q.rear+1)%MAXQSIZE==Q.front)
return ERROR;//队列满
Q.base[Q.rear]=e;
Q.rear = (Q.rear + 1)% MAXQSIZE;
return OK;
}
Status DeQueue (SqQueue Q,QElemType e){
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR
if (Q.front == Q.rear ) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1)% MAXQSIZE;
return OK;
}
void out_Q(SqQueue Q){
char ch;
if (Q.front == Q.rear ) printf(队列为空。);
else {printf(\n%d,*Q.base );
Q.base++;}
printf(\n 打回车键,继续。);
ch=getchar();
}/* out_Q */
void main(){
SqQueue Q;
int k;
char ch;
QElemType e;
InitQueue (Q);
do{
printf(\n\n\n);
printf(\n\n 1. 数据元素e进队列 );
printf(\n\n 2. 出队一个元素,返回其值);
printf(\n\n 3. 结束程序运行);
printf(\n======================================);
printf(\n 请输入您的选择 (1,2,3));
scanf(%d,k);
switch(k){
case 1:{
printf(\n 进队 e=?);
scanf(%d,e);
EnQueue(Q,e);
out_Q(Q);
} break;
case 2:{
DeQueue(Q,e);
printf(\n出队元素 : %d, e);
out_Q(Q);
} break;
case 3: break;
} /* switch */
printf(\n ----------------);
}while(k=1k3);
printf(\n 再见!);
printf(\n 打回车键,返回。);
ch=getchar();
}
实验程序执行结果:(实验结果可以手写,也可以截屏打印出来。)
实验过程中出现的问题及解决方法:
还是代码大小写容易出错,要仔细检查好几遍
对循环队列掌握不好,要好好看书
显示全部