顺序队列-数据结构(10)

一、解析

之所以叫队列,也是一个故名思意。排队中先进队伍的,顺序出去的,也就是从队头出去,而顺序的意思就是在内存结构中是顺序存放的,也就是数组。

二、存储结构

typedef struct{
	QElemType *base;//基地址
	int front;//队头
	int rear;//队尾 总是指向队尾的空位置
}SqQueue;

三、操作

//===============顺序队列 循环队列============
#define MAXQSIZE 5
typedef struct{
	QElemType *base;//基地址
	int front;//队头
	int rear;//队尾 总是指向队尾的空位置
}SqQueue;

Status InitQueue_Sq(SqQueue &Q){
	Q.base = (QElemType*)malloc(sizeof(QElemType)*MAXQSIZE);
	if (Q.base == NULL)
	{
		printf("分配失败");
		return ERROR;
	}
	Q.front = 0;
	Q.rear = 0;
	return OK;
}

Status EnQueue_Sq(SqQueue &Q, QElemType e){
	//进队e
	//队列满了的情况
	if ((Q.rear + 1) % MAXQSIZE == Q.front)
	{
		printf("队列满了");
		return ERROR;
	}
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1) % MAXQSIZE;
	return OK;
}

Status DeQueue_Sq(SqQueue &Q, QElemType &e){
	//出队 赋值给e
	//判断队列是否为空
	if (Q.front == Q.rear)
	{
		//队列为空
		printf("队列为空");
		return ERROR;
	}
	e = Q.base[Q.front];
	Q.front = (Q.front + 1) % MAXQSIZE;
	return OK;
}

四、执行

	//顺序队列
	SqQueue q;
	InitQueue_Sq(q);
	EnQueue_Sq(q,1);
	EnQueue_Sq(q, 2);
	EnQueue_Sq(q, 3);
	EnQueue_Sq(q, 4);
	EnQueue_Sq(q, 5);
	EnQueue_Sq(q, 6);
	int e;
	DeQueue_Sq(q,e);
	printf("%d\n",e);
	DeQueue_Sq(q, e);
	printf("%d\n", e);
	DeQueue_Sq(q, e);
	printf("%d\n", e);
	DeQueue_Sq(q, e);
	printf("%d\n", e);
	DeQueue_Sq(q, e);
	printf("%d\n", e);
	DeQueue_Sq(q, e);
	printf("%d\n", e);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值