【数据结构】链式队列的基本操作(C++)


前言

本文主要介绍数据结构中链式队列的基本操作


一、定义链式队列

typedef struct QueneNode
{
	int data;
	struct QueneNode* next;
}Node;
typedef struct LinkQuene
{
	Node* front;
	Node* rear;
}LinkQuene;

二、链式队列的基本操作

1.初始化队列

//初始化队列
void Init_Quene(LinkQuene& q)
{
	Node *p;
	p = new Node;
	if (!p)
	{
		cout << "分配内存失败" << endl;
		return;
	}
	p->next = NULL;
	q.front = q.rear = p;
}

2.判断队列是否为空

//判断队列是否为空
int is_Empty_Quene(LinkQuene q)
{
	if (q.front == q.rear)
	{
		return 1;
	}
	return 0;
}

3.入队操作

//入队操作
void Push_Quene(LinkQuene& q, int e)
{
	Node* p;
	p = new Node;
	if (!p)
	{
		cout << "分配内存失败" << endl;
		return;
	}
	p->data = e;
	p->next = NULL;
	q.rear->next = p;
	q.rear = p;
}

4.出队操作

//出队操作
void Pop_Quene(LinkQuene& q, int& e)
{
	if (is_Empty_Quene(q))
	{
		return;
	}
	Node* p;
	p = q.front->next;
	if (q.front->next==q.rear)
	{
		q.rear = q.front;
	}
	e = p->data;
	q.front->next = p->next;
	delete p;
}

5.访问队列的队首元素

//访问队列中的队首元素
void Get_Top(LinkQuene q, int& a)
{
	if (is_Empty_Quene(q))
		return;
	a = q.front->next->data;
}

6.计算队列的长度

//求队列长度
int Length_Quene(LinkQuene& q)
{
	int length = 0;
	Node* p = q.front->next;
	while(p != NULL)
	{
		length++;
		p = p->next;
	}
	return length;
}

所有源代码

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
typedef struct QueneNode
{
	int data;
	struct QueneNode* next;
}Node;
typedef struct LinkQuene
{
	Node* front;
	Node* rear;
}LinkQuene;
//初始化队列
void Init_Quene(LinkQuene& q)
{
	Node *p;
	p = new Node;
	if (!p)
	{
		cout << "分配内存失败" << endl;
		return;
	}
	p->next = NULL;
	q.front = q.rear = p;
}
//判断队列是否为空
int is_Empty_Quene(LinkQuene q)
{
	if (q.front == q.rear)
	{
		return 1;
	}
	return 0;
}
//入队操作
void Push_Quene(LinkQuene& q, int e)
{
	Node* p;
	p = new Node;
	if (!p)
	{
		cout << "分配内存失败" << endl;
		return;
	}
	p->data = e;
	p->next = NULL;
	q.rear->next = p;
	q.rear = p;
}
//出队操作
void Pop_Quene(LinkQuene& q, int& e)
{
	if (is_Empty_Quene(q))
	{
		return;
	}
	Node* p;
	p = q.front->next;
	if (q.front->next==q.rear)
	{
		q.rear = q.front;
	}
	e = p->data;
	q.front->next = p->next;
	delete p;
}
//访问队列中的队首元素
void Get_Top(LinkQuene q, int& a)
{
	if (is_Empty_Quene(q))
		return;
	a = q.front->next->data;
}
//求队列长度
int Length_Quene(LinkQuene& q)
{
	int length = 0;
	Node* p = q.front->next;
	while(p != NULL)
	{
		length++;
		p = p->next;
	}
	return length;
}

int main()
{
	LinkQuene q;
	Init_Quene(q);
	if (is_Empty_Quene(q))
	{
		cout << "该队列为空" << endl;
	}
	cout << "该队列的长度为:" << Length_Quene(q) << endl;

	Push_Quene(q, 1);
	Push_Quene(q, 2);
	Push_Quene(q, 3);
	int a = 0;
	Get_Top(q, a);
	cout << "队首元素为:" << a << endl;
	cout << "该队列的长度为:" << Length_Quene(q) << endl;
	int e = 0;
	Pop_Quene(q, e);
	Get_Top(q, a);
	cout << "队首元素为:" << a << endl;
	Pop_Quene(q, e);
	Get_Top(q, a);
	cout << "队首元素为:" << a << endl;

	Pop_Quene(q, e);
	

	cout << "该队列的长度为:" << Length_Quene(q) << endl;

	return 0;
}
  • 10
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

若鱼不是鱼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值