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

实验五 循环队列的基本运算

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");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值