顺序循环队列(C语言实现)

顺序循环队列(C语言实现)

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define MAXQSIZE 100
typedef int QElemtype;
typedef int Status;
typedef struct{
	QElemtype *base;
	int rear;
	int front;
}SqQueue;

Status InitQueue(SqQueue &Q){          //队列初始化 
	Q.base=(QElemtype *)malloc(sizeof(QElemtype)*MAXQSIZE);
    if(!Q.base)
    return ERROR;
    Q.front =Q.rear =0;
    printf("初始化成功\n");
    return OK;
} 

Status EmptyQueue(SqQueue Q){         //判断队列是否为空 
	if(Q.rear ==Q.front ){
	printf("队列为空\n");	
	return OK;}
	else{
    printf("队列不为空\n");
	return ERROR;	}
}

Status LengthQueue(SqQueue Q){             //求队列长度 
	return ((Q.rear -Q.front +MAXQSIZE)%MAXQSIZE);
}

Status EnQueue(SqQueue &Q,QElemtype &e){           //队列的入队 
	if((Q.rear+1)%MAXQSIZE==Q.front )
	return ERROR;
	Q.base[Q.rear ]=e;
	Q.rear=(Q.rear +1)%MAXQSIZE;
	printf("入队成功\n");
	return OK;
}

Status DeQueue(SqQueue &Q,QElemtype &e){             //队列的出队 
	if(Q.front ==Q.rear)
	return ERROR;
	e=Q.base[Q.front ];
	Q.front =(Q.front+1)%MAXQSIZE;
	return OK; 
}

Status GetHead(SqQueue Q){                  //获取队头元素 
	if(Q.front !=Q.rear )
	return Q.base[Q.front ];
}

int main(){
	SqQueue Q;
	InitQueue(Q);
	EmptyQueue(Q);
	printf("队列长度为%d\n",LengthQueue(Q));
	QElemtype a=1,e;
	EnQueue(Q,a);
	printf("栈顶元素为%d\n",GetHead(Q));
	printf("出队数据为%d\n",DeQueue(Q,e));
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值