循环队列

本文介绍了一种基于数组实现的循环队列及其基本操作,包括初始化、清空、判断是否为空或满、获取队列长度、遍历元素、入队、出队以及读取队头和队尾元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef SQQUEUE_H
#define SQQUEUE_H
#include"head.h"
#define MAXQSIZE 8
typedef int ElemType;
typedef struct{
	ElemType *base;
	int head;
	int tail;
}SqQueue;

bool InitQueue(SqQueue &Q){
	Q.base=(ElemType*)malloc((MAXQSIZE)*sizeof(ElemType));
	if(!Q.base) exit(OVERFLOW);
	Q.head=Q.tail=0;
	return OK;
}
bool ClearQueue(SqQueue &Q){
	Q.head=Q.tail=0;
	return OK;
}
bool IsEmpty(SqQueue Q){
	return Q.head==Q.tail?1:0;
}
bool IsFull(SqQueue Q){
	return (Q.tail+1-Q.head)%MAXQSIZE?0:1;
}
int QueueLength(SqQueue Q){
	return (Q.tail-Q.head+MAXQSIZE)%MAXQSIZE;
}
bool QueueTraverse(SqQueue Q){
	printf("the queue from head to tail is:");
	for(int i=Q.head;i!=Q.tail;(++i)%MAXQSIZE){
		printf("%d,",Q.base[i]);
	}
	printf("\n");
	return OK;
}
bool EnQueue(SqQueue &Q,ElemType e){
	if(IsFull(Q)) return ERROR;
	Q.base[Q.tail++]=e;
	Q.tail%=MAXQSIZE;
	return OK;
}
bool DeQueue(SqQueue &Q,ElemType &e){
	if(IsEmpty(Q)) return ERROR;
	e=Q.base[Q.head++];
	Q.head%=MAXQSIZE;
	return OK;
}
bool GetHead(SqQueue &Q,ElemType &e){
	if(IsEmpty(Q)) return ERROR;
	e=Q.base[Q.head];
	return OK;
}
bool GetTail(SqQueue &Q,ElemType &e){
	if(IsEmpty(Q)) return ERROR;
	e=Q.base[(Q.tail-1+MAXQSIZE)%MAXQSIZE];
	return OK;
}
#endif
//SqQueue.h

#ifndef _HEAD 
#define _HEAD

#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INFEASIBLE -1
//typedef bool{false,true} bool;

#endif
//head.h


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值