队列-顺序结构-静态存储

队列-顺序结构-静态存储

Status Init_SeqQueue(&Q); 初始化操作
Status Length_SeqQueue(&Q); 求表长
ElemType Get_SeqQueue(&Q); 取队首元素
Status Add_SeqQueue(&Q,e); 入队操作
ElemType De_SeqQueue(&Q); 出队操作
Status Empty_SeqQueue(&Q); 判空

SeqQueue.h

/*
*项目名: List
*文件名: SeqQueue.h
*创建者: CaoPengCheng
*创建时间:2021-4-14
*描述: 循环队列(静态)
*函数:
	Status Init_SeqQueue(&Q); 初始化操作
	Status Length_SeqQueue(&Q); 求表长
	ElemType Get_SeqQueue(&Q); 取队首元素
	Status Add_SeqQueue(&Q,e); 入队操作
	ElemType De_SeqQueue(&Q); 出队操作
	Status Empty_SeqQueue(&Q); 判空
*/

#include "stdio.h"
#include "stdlib.h"

#define ElemType int
#define Status int 
#define MAXSiZE 100 
#define OK 1
#define ERROE 0

typedef struct {
	ElemType data[MAXSiZE];
	int front,rear;
}SeqQueue;

//初始化
Status Init_SeqQueue(SeqQueue *Q){
	Q->front=0;
	Q->rear=0;
	return OK;
}

//判空
Status Empty_SeqQueue(SeqQueue *Q){
	if(Q->front ==Q->rear )
		return OK;
	else
		return ERROE ;
}

//入队
Status Add_SeqQueue(SeqQueue *Q,ElemType  e){
	if((Q->rear+1)%MAXSiZE==Q->front){
		printf("队满!\n");
		return ERROE;
	}
	Q->rear =(Q->rear +1)%MAXSiZE ;
	Q->data[Q->rear]=e;
	return OK;
}

//取队头
ElemType  Get_SeqQueue(SeqQueue *Q){
	ElemType e;
	if(Empty_SeqQueue(Q)){
		printf("队空!\n");
		return ERROE;
	}
	e=Q->data [(Q->front +1)%MAXSiZE ];
	return e;
}

//出队
ElemType  De_SeqQueue(SeqQueue *Q){
	ElemType e;
	if(Empty_SeqQueue(Q)){
		printf("队空!\n");
		return ERROE;
	}
	Q->front =(Q->front +1)%MAXSiZE;//出队
	e=Q->data[Q->front];
	return e;
}

//求队长度
int Length_SeqQueue(SeqQueue *Q){
	return (Q->rear-Q->front+MAXSiZE)%MAXSiZE;
}

测试

Status SeqQueueTest(){
	/*
	Status Init_SeqQueue(&Q); 初始化操作
	Status Length_SeqQueue(&Q); 求表长
	ElemType Get_SeqQueue(&Q); 取队首元素
	Status Add_SeqQueue(&Q,e); 入队操作
	ElemType De_SeqQueue(&Q); 出队操作
	Status Empty_SeqQueue(&Q); 判空
*/
	SeqQueue *Q,q;
	int i;
	Q=&q;
	printf("---循环队列测试---\n");
	Init_SeqQueue(Q);

	printf("入队");
	for(i=20;i>=0;i--)
		Add_SeqQueue(Q,i);
	printf("Success!!\n");

	printf("队头:%d \n",Get_SeqQueue(Q));
	//队尾会占用一个节点,所有长度一个比实际长度加一
	printf("队长:%d \n",Length_SeqQueue(Q));
	printf("出队:");
	for(i=1;i<=20;i++)
		printf("%d ",De_SeqQueue(Q));
	printf("\nover!\n");

}

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CaoPengCheng&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值