队列的基本操作(数据结构)

1.实验内容:

编写一个程序sqqueue.cpp,实现环形队列(假设栈中元素类型ElemType 为 char)的各种基本运算,并在此基础上设计一个程序exp4_1.cpp,完成如下功能:

2.实验步骤:

(1)初始化队列q

(2)判断队列q是否非空

(3)依次进队元素a、b、c

(4)出队一个元素,输出该元素

(5)依次进队元素d、e、f

(6)输出出队序列

(7)释放队列。

3.实验代码:

创建一个文件命名为sqstack.cpp,在文件下放代码:

#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{ElemType data[MaxSize];
int front,rear;
}SqQueue;
void InitQueue(SqQueue *&q){    //初始化环形队列q 
    q=(SqQueue *)malloc(sizeof(SqQueue));
    q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q){    //销毁环形队列 
	free(q);
}
bool QueueEmpty(SqQueue *q){
	return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e){    //进队列 
	if ((q->rear+1)%MaxSize==q->front)
	    return false;    //队列满 
	q->rear=(q->rear+1)%MaxSize;
	q->data[q->rear]=e;
	return true;
}
bool deQueue(SqQueue *&q,ElemType &e){    //出队列 
	if (q->rear==q->front)
		return false;    //队空 
	q->front=(q->front+1)%MaxSize;
	e=q->data[q->front];
	return true; 
}

 创建另一个文件,在文件下放代码:

#include "sqstack.cpp"
int main()
{
	ElemType e;
	SqQueue *q;
	printf("环形队列基本运算如下:\n");
	printf("(1)初始化队列q\n");
	InitQueue(q);
	printf("(2)依次进队元素a,b,c\n");
	if(!enQueue(q,'a')) printf("\t提示:队满,不能进队\n"); 
	if(!enQueue(q,'b')) printf("\t提示:队满,不能进队\n");
	if(!enQueue(q,'c')) printf("\t提示:队满,不能进队\n");
	printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空"));
	if(deQueue(q,e)==0)
		printf("队空,不能出队\n");
	else
		printf("(4)出队一个元素%c\n",e);
	printf("(5)依次进队列元素d,e,f\n");
	if(!enQueue(q,'d')) printf("\t 提示:队满,不能进队\n");
	if(!enQueue(q,'e')) printf("\t 提示:队满,不能进队\n");
	if(!enQueue(q,'f')) printf("\t 提示:队满,不能进队\n");
	printf("(6)出队列序列:");
	while(!QueueEmpty(q))
	{
		deQueue(q,e);
		printf("%c",e);
	}
	printf("\n");
	printf("(7)释放队列\n");
	DestroyQueue(q);
	return 1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿勉要睡觉(考试版)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值