数据结构作业(第三章第一题)

设计一个循环队列QUEUE<T>,用data[0..MaxSize-1]存放队列元素,用front和rear分别作为队头和队尾指针,另外用一个标志tag标识队列可能空(false)或可能满(true),这样加上front==rear可以作为队空或队满的条件。要求设计队列的相关基本运算算法。

#include<stdio.h>
#include <corecrt_malloc.h>
#define MAXSIZE 5
typedef struct {
	//const static int MAXSIZE = 5;
	int data[MAXSIZE];
	int front, rear, tag;
}Queue;
void InitQueue(Queue* queue) {
	queue->front = 0;
	queue->rear = 0;
	queue->tag = 0;
}
int QueueEmpty(Queue *queue) {
	return(queue->front == queue->rear && queue->tag == 0);
}
void DestroyQueue(Queue *queue) {
	free(queue);
}

int enQueue(Queue* queue, int a) {
	if (queue->front == queue->rear && queue->tag == 1) {
		return 0;
	}
	queue->rear = (queue->rear + 1) % MAXSIZE;
	queue->data[queue->rear] = a;
	queue->tag = 1;
	return 1;
}
int deQueue(Queue* queue, int* a) {
	if (queue->front == queue->rear && queue->tag == 0) {
		return 0;
	}
	*a = queue->data[queue->front + 1];
	queue->front = (queue->front + 1) % MAXSIZE;
	queue->tag = 0;
	return 1;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值