您现在的位置是:群英 > 开发技术 > 编程语言
C++队列概念及结构是啥,怎样实现队列?
Admin发表于 2022-02-16 18:09:041117 次浏览

    这篇文章我们来了解C++队列的相关内容,下文给大家介绍了队列概念和结构,以及怎样实现队列,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!

    1. 队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头


    2. 队列的实现

    2.1 queue.h

#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
#include<malloc.h>
typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode*next;
	QDataType data;
}QueueNode;
typedef struct Queue
{
	QueueNode *head;
	QueueNode *tail;
}Queue;
void QueueInit(Queue *pq);
void QueueDestory(Queue *pq);
void QueuePush(Queue *pq,QDataType x);
void QueuePop(Queue *pq);
QDataType QueueFront(Queue *pq);
QDataType QueueBack(Queue *pq);
bool QueueEmpty(Queue *pq);
int QueueSize(Queue *pq);

    2.2 queue.c

#include"queue.h"
void QueueInit(Queue *pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}
void QueueDestory(Queue *pq)
{
	assert(pq);
	QueueNode *cur = pq->head;
	while (cur)
	{
		QueueNode *next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}
void QueuePush(Queue *pq, QDataType x)
{
	assert(pq);
	QueueNode *newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePop(Queue *pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QueueNode *next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}
QDataType QueueFront(Queue *pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}
QDataType QueueBack(Queue *pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}
bool QueueEmpty(Queue *pq)
{
	assert(pq);
	return pq->head == NULL;
}
int QueueSize(Queue *pq)
{
	int size = 0;
	QueueNode *cur = pq->head;
	while (cur)
	{
		QueueNode *next = cur->next;
		++size;
		cur = cur->next;
	}
	return size;
}

    2.3 test.c

#include"queue.h"
void TestOne()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	while (!QueueEmpty(&q))
	{
		printf("%d  ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
	QueueDestory(&q);
}
int main()
{
	TestOne();
	return 0;
}

    总结

    现在大家对于C++队列概念、结构和实现应该都清楚了吧,希望大家阅读完这篇文章能有所收获。最后,想要了解更多C++队列的内容,大家可以关注群英网络其它相关文章。

文本转载自PHP中文网

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: c 队列
相关信息推荐
2022-06-16 17:01:58 
摘要:在准备学go的小伙伴,一开始都会直接在Windows上安装使用,今天给大家写一份在Linux上的安装教程。
2022-01-04 18:53:13 
摘要:今天给大家分享的是Python基础练习之求未知个数的平均数,这里需要使用到sun函数求平均数。首先给大家分享一个已知十个数,求平均数的示例,然后我们再来看未知个数的平均数怎样求。
2022-09-05 17:27:13 
摘要:区别:1、动态组件是Vue中一个特殊的Html元素“<component>”,它拥有一个特殊的is属性,属性值可以是“已注册组件的名称”或“一个组件的选项对象”;而异步组件不是实物,是一个概念,一个可以让组件异步加载的方式。2、动态组件用于不同组件之间进行动态切换;而异步组件用于性能优化,比如减小首屏加载时间、加载资源大小。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部