顺序队列的实现

#include<stdio.h>
#include<stdlib.h>

#define OK 1
#define ERROR 0
#define QUEUE_INIT_SIZE 5

typedef int Status;
typedef int Elemtype;
typedef struct QuqQueue{
	Elemtype* base;
	int front;
	int rear;
}SuqQueue;

Status InitQueue(SuqQueue *sq){
	sq->base = (Elemtype*)malloc(sizeof(Elemtype) * QUEUE_INIT_SIZE);
	if( !sq->base )
		return ERROR;
	sq->front = sq->rear = 0;
	return OK;
}
Status InsertQueue(SuqQueue *sq,Elemtype value){
	if( (sq->rear + 1) % QUEUE_INIT_SIZE == sq->front){
		printf("OVERFLOW!\n");
		return ERROR;
	}
	sq->base[sq->rear] = value;
	sq->rear = ( sq->rear + 1 ) % QUEUE_INIT_SIZE;
	return OK;
}
Status DeleteQueue(SuqQueue *sq,Elemtype* value){
	if( sq->front == sq->rear ){
		printf("EMPTY!\n");
		return ERROR;
	}
	*value = sq->base[sq->front];
	sq->front = ( sq->front +  1 ) % QUEUE_INIT_SIZE;
	return OK;
}
int main(){
	SuqQueue s;
	InitQueue(&s);
	InsertQueue(&s,1);
	InsertQueue(&s,2);
	InsertQueue(&s,3);
	InsertQueue(&s,4);
	Elemtype r;
	DeleteQueue(&s,&r);
	printf("%d ", r);
	DeleteQueue(&s,&r);
	printf("%d ", r);
	DeleteQueue(&s,&r);
	printf("%d ", r);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值