队列(链式存储)基本操作

目录:

1.队列结构体定义

2.判断队空

3.入队

4.出队

5.取队头元素

6.遍历队列

注:队列链式存储不需要判断队满

下面是队列链式存储(带头结点)的实现

//带头节点的链式存储的队列
/**/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
	ElemType data;
	struct LinkNode* next;
}LinkNode;
typedef struct{
	LinkNode* front, * rear;//队列对头和队尾的指针
}LinkQueue;
//初始化队列(带头节点)
void InitQueue(LinkQueue& Q) {
	Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
	Q.front->next = NULL;
}
//判断队空
bool IsEmpty(LinkQueue Q) {
	if (Q.front == Q.rear)
		return true;
	else
		return false;
}
//不需要判断队满
//新元素入队
void EnQueue(LinkQueue& Q, ElemType x) {
	LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
	s->data = x;
	s->next = NULL;
	Q.rear->next = s;
	Q.rear = s;
}
//出队
bool DeQueue(LinkQueue& Q, ElemType& x) {
	if (Q.front == Q.rear)
		return false;
	LinkNode* p = Q.front->next;
	x = p->data;
	Q.front->next = p->next;
	if (Q.rear == p){
		Q.rear = Q.front;
	}
	free(p);
	return true;
}
//获取队头元素
bool GetTop(LinkQueue& Q, ElemType &x) {
	if (Q.front == Q.rear)
		return false;
	x = Q.front->next->data;
	return true;
}
//遍历队列
void  ShowQueue(LinkQueue Q) {
	LinkNode* p = Q.front->next;
	while (p != NULL) {
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}
int main() {
	LinkQueue Q;
	int x,e,y;
	InitQueue(Q);
	//printf("请输入队列元素个数:\n");
	printf("请依次输入队列元素:\n");
	scanf_s("%d", &x);
	while (x != 9999) {
		EnQueue(Q, x);
		scanf_s("%d", &x);
	}
	if(IsEmpty(Q)==1){
		printf("队列空!\n");
	} 
	else{
		printf("队列不空!\n");
	}
	printf("此时队列元素为:\n");
	ShowQueue(Q);
	//出队
	printf("下面是出队操作\n");
	DeQueue(Q, e);
	printf("出队的元素是%d\n", e);
	printf("出队操作后的队列为:\n");
	ShowQueue(Q);
	//取队头元素
	GetTop(Q, y);
	printf("此时的队头元素为:%d\n", y);
	return 0;
}

下面是队列链式存储(不带头结点)的实现:

//不带头节点的链式存储的队列
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
	ElemType data;
	struct LinkNode* next;
}LinkNode;
typedef struct {
	LinkNode* front, * rear;
	//int length;
}LinkQueue;
//初始化
void InitQueue(LinkQueue& Q) {
	Q.front = NULL;
	Q.rear = NULL;
}
//判空
bool IsEmpty(LinkQueue Q) {
	if (Q.front == NULL)
		return true;
	else
		return false;
}
//新元素入队
void EnQueue(LinkQueue& Q, ElemType x) {
	LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
	s->data=x;
	s->next = NULL;
	//第一个入队的元素单独讨论
	if (Q.front == NULL) {
		Q.front = s;
		Q.rear = s;
	}
	Q.rear->next = s;
	Q.rear = s;
}
//出队
bool DeQueue(LinkQueue& Q, ElemType& x) {
	if (Q.front == NULL)
		return false;
	LinkNode* p = Q.front;
	x = p->data;
	Q.front = p->next;
	//队列中只有一个元素的出队单独讨论
	if (Q.front==p) {
		Q.rear = NULL;
		Q.front = NULL;
	}
	free(p);
	return true;
}
//获取队列队尾元素
bool GetTop(LinkQueue& Q, ElemType& x) {
	if (Q.front == NULL)
		return false;
	x = Q.front->data;
	return true;
}
//遍历队列
void  ShowQueue(LinkQueue Q) {
	LinkNode* p = Q.front;
	while (p != NULL) {
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}
int main() {
	LinkQueue Q;
	InitQueue(Q);
	int x;
	int e, y;
	//printf("请输入第一个进队列的元素:\n");
	//scanf_s("%d", &x);
	printf("请依次输入队列元素:\n");
	scanf_s("%d", &x);
	while(x!=9999){
		EnQueue(Q, x);
		scanf_s("%d", &x);
	}
	//printf("\n");
	printf("此时队列元素为:\n");
	ShowQueue(Q);
	//出队
	printf("下面是出队操作\n");
	DeQueue(Q, e);
	printf("出队的元素是%d\n", e);
	printf("出队操作后的队列为:\n");
	ShowQueue(Q);
	//取队头元素
	GetTop(Q, y);
	printf("此时的队头元素为:%d\n", y);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

自律的光电人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值