顺序队列

#include <stdio.h>
#include <windows.h>
//定义一个顺序队列
#define QUEUESIZE 100
typedef struct Squeue {
	int queue[QUEUESIZE];
	int front;
	int rear;
} SeQueue;
//顺序队列的初始化
void InitQueue(SeQueue* S) {
	S->front = S->rear = 0;
}
//判断队列是否为空
bool IsEmpty(SeQueue S) {
	if (S.front == S.rear) {
		return 1;
	}
	return 0;
}
//入队操作
bool InQueue(SeQueue* S, int e) {
	//判断队列是否为满
	if (S->rear >= QUEUESIZE) {
		return 0;
	}
	S->queue[S->rear] = e;
	++S->rear;
	return 1;
}


//出队操作
bool OutQueue(SeQueue* S, int* e) {
	//判断队列是否为空
	if (IsEmpty(*S)) {
		return 0;
	}
	*e = S->queue[S->front];
	++S->front;
	return 1;
}

//销毁队列
void DestoryQueue(SeQueue *Q) {
	Q->front=Q->rear=0;

}

//获取队头元素
int GetEle(SeQueue Q) {
	if(Q.front==Q.rear) {
		return 0;
	}
	int value = Q.queue[Q.front];
	return value;
}

//打印队列
void Display(SeQueue s1) {
	if(s1.front==s1.rear) {
		printf("队列为空!\n");
		return ;
	}

	while (s1.front != s1.rear) {
		printf("Q[%d]=%d\n", s1.front, s1.queue[s1.front]);
		s1.front = s1.front + 1;
	}
}

int main() {
	SeQueue s1;
	InitQueue(&s1);
	for (int i = 0; i < 10; ++i) {
		if (InQueue(&s1, i + 1));
	}
	Display(s1);
	printf("\n");
printf("当前队列的头元素为%d\n",GetEle(s1));
	//出队
	int e =0;
	for (int i = 0; i < 5; ++i) {
		if (OutQueue(&s1, &e)) {
			printf("Q[%d]=%d\n", i,e);
		}
	}
	printf("\n");
	//获取队列头元素
	
	printf("\n");
	printf("\n");
	//打印队列
	Display(s1);
	printf("\n");
	printf("当前队列的头元素为%d\n",GetEle(s1));
	printf("\n");

	//销毁队列
	printf("\n");
	DestoryQueue(&s1);
	Display(s1);
	printf("当前队列的头元素为%d\n",GetEle(s1));
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值