队列顺序存储C语言实现

参考资料为《大话数据结构》

typedef int ElemType;
typedef struct {
	ElemType data[MAXSIZE];
	int front, rear;
}SqQueue;
//让front指向队头元素,rear指向队尾元素的下一个位置(无值)

1、初始化

//初始化
void InitQueue(SqQueue* Q) {
	Q->front = 0;
	Q->rear = 0;
}

2、清除队列

//清除队列
void ClearQueue(SqQueue* Q) {
	Q->front = 0;
	Q->rear = 0;
}

3、判断是否为空

//判断是否为空
bool QueueEmpty(SqQueue Q) {
	if (Q.front == Q.rear) {
		return true;
	}
	else {
		return false;
	}
}

4、返回队列长度

//返回队列长度
int QueueLength(SqQueue Q) {
	return  (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

若rear>front且≤MAXSIZE,则Q.rear - Q.front即队列长度(后面加上MAXSIZE又%MAXSIZE相抵消了)
在这里插入图片描述
因为队列元素在队尾进入,队头出队,因此有可能出现rear<front的情况出现(当队尾存满,队头又有元素出队之后,front不在data[0]位置,rear可以重新从data[0]开始)
在这里插入图片描述

5、返回队头元素

//返回队头元素
bool GetHead(SqQueue Q, ElemType* e) {
	if (Q.rear == Q.front) {
		return false;
	}
	else {
		*e = Q.data[Q.front];
		return true;
	}
}

6、插入元素

//插入元素
bool EnQueue(SqQueue* Q, ElemType e) {
	if ((Q->rear + 1) % MAXSIZE == Q->front) {//队列满
		return false;
	}
	else {
		Q->data[Q->rear] = e;
		Q->rear = (Q->rear + 1) % MAXSIZE;
		return true;
	}
}

并不是rear==MAXSIZE的时候队列就是满的,有可能在队尾出去了几个元素,那么是还可以继续存放的
这时候就需要rear重新指回到下标为0的位置
取模操作让无限整数域能够映射到有限的整数域中(该设置下为MAXSIZE范围内的数)
逻辑上可以看作是一个循环队列,rear在到队头之后可以重新回到起点
那么当rear+1并取模之后的指向与front重合则说明队列已满
但是重合的状况我们是不能让它出现的
否则会和前面判断是否为空等操作冲突,误以为队列为空
因此需要舍弃最后一个存储位置,来让rear指向它
因此若rear+1取模后与front重合,是直接返回false,而不是填满最后一个存储空间后返回

7、元素出队

//元素出队
bool DeQueue(SqQueue* Q, ElemType* e) {
	if (Q->rear == Q->front) {//队列空
		return false;
	}
	else {
		*e = Q->data[Q->front];
		Q->front = (Q->front + 1) % MAXSIZE;//取模才能让front循环起来,同上理
		return true;
	}
}

8、输出

//输出元素
void ShowQueue(SqQueue Q) {
	int i = Q.front; 
	while ((i + Q.front) != Q.rear) {
		printf("%d ", Q.data[i]);
		i = (i + 1) % MAXSIZE;
	}
	printf("\n");
}

9、完整+测试程序

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdbool.h>
#include <stdio.h>
#define MAXSIZE 10
typedef int ElemType;
typedef struct {
	ElemType data[MAXSIZE];
	int front, rear;
}SqQueue;
//让front指向队头元素,rear指向队尾元素的下一个位置(无值)

//初始化
void InitQueue(SqQueue* Q) {
	Q->front = 0;
	Q->rear = 0;
}

//清除队列
void ClearQueue(SqQueue* Q) {
	Q->front = 0;
	Q->rear = 0;
}

//判断是否为空
bool QueueEmpty(SqQueue Q) {
	if (Q.front == Q.rear) {
		return true;
	}
	else {
		return false;
	}
}

//返回队列长度
int QueueLength(SqQueue Q) {
	return  (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

//返回队头元素
bool GetHead(SqQueue Q, ElemType* e) {
	if (Q.rear == Q.front) {
		return false;
	}
	else {
		*e = Q.data[Q.front];
		return true;
	}
}

//插入元素
bool EnQueue(SqQueue* Q, ElemType e) {
	if ((Q->rear + 1) % MAXSIZE == Q->front) {//队列满
		return false;
	}
	else {
		Q->data[Q->rear] = e;
		Q->rear = (Q->rear + 1) % MAXSIZE;
		return true;
	}
}

//元素出队
bool DeQueue(SqQueue* Q, ElemType* e) {
	if (Q->rear == Q->front) {//队列空
		return false;
	}
	else {
		*e = Q->data[Q->front];
		Q->front = (Q->front + 1) % MAXSIZE;//取模才能让front循环起来,同上理
		return true;
	}
}

//输出元素
void ShowQueue(SqQueue Q) {
	int i = Q.front; 
	while ((i + Q.front) != Q.rear) {
		printf("%d ", Q.data[i]);
		i = (i + 1) % MAXSIZE;
	}
	printf("\n");
}

//TEST
int main(void) {
	SqQueue Q;
	InitQueue(&Q);
	printf("现在队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));
	int flag;
	int i = 0;
	int l;
	ElemType d;
	printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ", MAXSIZE - 1);
	do
	{
		scanf("%d",&d); 
		//d = i + 100;
		if (d == -1)
			break;
		i++;
		EnQueue(&Q, d);
	} while (i < MAXSIZE - 1);
	printf("现在队列为:\n");
	ShowQueue(Q);
	printf("队列长度为: %d\n", QueueLength(Q));
	printf("现在队列空否?%u(1:空 0:否)\n", QueueEmpty(Q));
	while (QueueLength(Q) > 2)
	{
		DeQueue(&Q, &d);
		printf("删除的元素值为%d\n", d);
	}
	printf("队列长度为: %d\n", QueueLength(Q));
	/*printf("现在队列为:\n");
	ShowQueue(Q);*/
	flag = GetHead(Q, &d);
	if (flag)
		printf("现在队头元素为: %d\n", d);
	return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 C 语言实现顺序队列的代码示例: ```c #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int front; int rear; } SqQueue; // 初始化队列 void InitQueue(SqQueue *q) { q->front = q->rear = 0; } // 判断队列是否为空 int QueueEmpty(SqQueue q) { return q.front == q.rear; } // 入队 int EnQueue(SqQueue *q, int x) { // 判断队列是否已满 if ((q->rear + 1) % MAXSIZE == q->front) { return 0; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; return 1; } // 出队 int DeQueue(SqQueue *q, int *x) { // 判断队列是否为空 if (q->front == q->rear) { return 0; } *x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } int main() { SqQueue q; int i, x; // 初始化队列 InitQueue(&q); // 入队 for (i = 1; i <= 10; i++) { EnQueue(&q, i); } // 出队 while (!QueueEmpty(q)) { DeQueue(&q, &x); printf("%d ", x); } printf("\n"); return 0; } ``` 该代码中使用了一个结构体 `SqQueue` 存储队列元素,队列的最大长度为 `MAXSIZE`。该结构体包含一个数组 `data` 用来存储队列元素,以及两个指针 `front` 和 `rear` 分别指向队头和队尾。 `InitQueue` 函数用于初始化队列,将 `front` 和 `rear` 都设置为 0。 `QueueEmpty` 函数用于判断队列是否为空,若队头指针和队尾指针相等,则说明队列为空。 `EnQueue` 函数用于入队,判断队列是否已满后,将元素插入到队尾,然后将队尾指针加 1。 `DeQueue` 函数用于出队,判断队列是否为空后,将队头元素保存到 `x` 中,然后将队头指针加 1。 在 `main` 函数中,我们先初始化队列,然后循环入队,最后循环出队并输出队列元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值