栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[逆向|C语言]C语言数据结构之顺序队列

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[逆向|C语言]C语言数据结构之顺序队列

目录

一.构建队列结构体

 二.队列添加数据

三.遍历显示队列

四.队列删除数据

 五.其他功能

1.判断队列是否为空

2.判断队列是否已满

3.清空队列

4.获取队列长度

5. 获取队列第一个成员


 

前言:顺序队列跟上篇文章介绍的顺序堆栈用法差不多,只不过堆栈的规则是先进后出,而队列的规则是先进先出,今天文章讲讲顺序队列.

一.构建队列结构体
#include "stdafx.h"
#define num 5
struct Queue
{
	int data[num];
	int front;
	int rear;

};

 二.队列添加数据
void InsertQueue(Queue*pQueue,int value)
{
	pQueue->data[pQueue->rear]=value;
	(pQueue->rear)++;

}

三.遍历显示队列
void bianliQueue(Queue* pQueue)
{
	int i;
	for(i=pQueue->front;irear;i++)
	{
		printf("%dn",pQueue->data[i]);


	}

添加了几个数据遍历显示如下图所示:

四.队列删除数据
bool DelQueue(Queue* pQueue, int* value)
{
	int i;
	if (pQueue->front==pQueue->rear)
	{
		return false;
	}
	*value=pQueue->data[pQueue->front];
	for (i=pQueue->front;irear-1;i++)
	{
		pQueue->data[i]=pQueue->data[i+1];
		
	}
	(pQueue->rear)--;
	return true;

}

 删除数据的代码如上所示,先判断里面有没有数据,没有数据时最前面是等于最后面的,最后注意指针别忘了自减.

运行后再遍历如下图所示

 五.其他功能

1.判断队列是否为空
bool emptyQueue(Queue* pQueue)
{
	if (pQueue->front==pQueue->rear)
	{
		printf("队列是空的n");
		return true;
	}
	else
	{
		return false;
	}

}

2.判断队列是否已满
bool fullQueue(Queue* pQueue)
{
	if (pQueue->rear-pQueue->front==num)
	{
		printf("队列已满n");
		return true;
	}
	else
	{
		return false;
	}


}

3.清空队列
void clearQueue(Queue* pQueue)
{
	pQueue->front=pQueue->rear;

}

4.获取队列长度
void queuelenth(Queue* pQueue,int*length)
{
	*length=pQueue->rear-pQueue->front;
}

5. 获取队列第一个成员
bool gethead(Queue* pQueue,int*head_value)
{
	if (emptyQueue(pQueue)==true)
	{
		return false;
	}
	*head_value=pQueue->data[pQueue->front];
	return true;
}

今天的文章就到这里了,如果有任何不明白的地方欢迎与我交流,我必定知无不言。这篇文章也花了一定的心血,喜欢的小伙伴可以点赞关注哦。感激不尽!  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022310.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号