数据结构(链队列

1. 讲解:

在这里插入图片描述

2. C++代码实现:

#include <stdlib.h>
#include <iostream>

using namespace std;

#define ElemType int

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;
}

// 入队
bool EnQueue(LinkQueue& Q, ElemType e) {
	LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
	if (s == NULL) return false;
	s->data = e;
	s->next = NULL;
	Q.rear->next = s;
	Q.rear = s;
}

// 出队
bool DeQueue(LinkQueue& Q, ElemType& e) {
	if (IsEmpty(Q)) return false;	// 空队

	LinkNode* s = Q.front->next;
	Q.front->next = s->next;
	e = s->data;
	if (s == Q.rear) {				// 最后一个元素出队时
		Q.rear = Q.front;
	}
	free(s);
	return true;
}

int main() {
	LinkQueue Q;
	InitQueue(Q); // 初始化队列

	// 入队操作
	for (int i = 1; i <= 5; ++i) {
		EnQueue(Q, i * 10);
		cout << "入队元素:" << i * 10 << endl;
	}

	// 出队操作
	ElemType e;
	while (!IsEmpty(Q)) {
		DeQueue(Q, e);
		cout << "出队元素:" << e << endl;
	}

	return 0;
}

小结:

关注我给大家分享更多有趣的知识,以下是个人公众号,提供 ||代码兼职|| ||代码问题求解||
添加我的公众号即可:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天玑y

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

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

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

打赏作者

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

抵扣说明:

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

余额充值