【队列】的实现

🖊作者 : D. Star.
📘专栏 : 数据结构
😆今日分享 :"混账"的由来 :
----------从前,蒙古族过着群居的游牧生活,白天,男人们出去放牧,只留下老人或妇女看守帐篷。由于种种因素,留在家的年轻小伙子们为了找姑娘谈情说爱,便混进姑娘们的帐篷里去。有时候,老人会愤怒地骂上句“你又混帐来了”“混帐东西又来了”。小伙子讨个没趣,也就急忙退出来。久而久之,“混账”一词演化为一句骂人的话,与当初的“混进帐篷”已无关系了。
请添加图片描述

请添加图片描述

✔头文件-声明:

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<assert.h>

typedef int QDataType;

typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail; 
	int size;
}Queue;

//初始化
Queue* InitQueue();

//销毁
void DestroyQueue(Queue* pq);

//入队
void PushQueue(Queue* pq,QDataType x);

//出队
void PopQueue(Queue* pq);

//取头
QDataType QueueFront(Queue* pq);

//取尾
QDataType QueueBack(Queue* pq);

//判断是否为空
bool EmptyQueue(Queue* pq);

//队列的长度
int SizeQueue(Queue* pq);

✔队列功能实现:

🔎初始化:

//初始化
Queue* InitQueue()
{
	Queue* pq = (Queue*)malloc(sizeof(Queue));
	if (pq == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
	return pq;
}

🔎销毁:

//销毁
void DestroyQueue(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* del = cur;
		cur = cur->next;
		free(del);
	}
	pq->head =pq->tail = NULL;
	pq->size = 0;
}

🔎入队:

//入队
void PushQueue(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		perror("malloc fail");;
		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;
	}
	pq->size++;
}

🔎出队:

//出队
void PopQueue(Queue* pq)
{
	assert(pq);
	assert(!EmptyQueue(pq));

	if (pq->head != pq->tail)
	{
		QNode* del = pq->head;
		pq->head = pq->head->next;	
		free(del);
	}
	else
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	
	pq->size--;
}

🔎取头:

//取头
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!EmptyQueue(pq));

	return pq->head->data;	
}

🔎取尾:

//取尾
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!EmptyQueue(pq));

	return pq->tail->data;
}

🔎判断是否为空:

//判断是否为空
bool EmptyQueue(Queue* pq)
{
	assert(pq);

	return pq->head == NULL && pq->tail == NULL;
}

🔎队列的长度:

//队列的长度
int SizeQueue(Queue* pq)
{
	assert(pq);
	时间复杂度为N;
	//int size = 0;
	//QNode* cur = pq->head;
	//while (cur)
	//{
	//	cur = cur->next;
	//	size++;
	//}
	
	//时间时间复杂度为1;
	return pq->size;
}

✔测试文件

#define _CRT_SECURE_NO_WARNINGS
#include"QueueNode.h"
//队列

test01()
{
	Queue* pq = InitQueue();

	PushQueue(pq, 1);
	PushQueue(pq, 2);
	PushQueue(pq, 3);
	PushQueue(pq, 4);
	printf("%d\n", QueueFront(pq));//1

	//PopQueue(pq);
	printf("%d\n", QueueBack(pq));//4
	PopQueue(pq);//2 3 4
	PopQueue(pq);//3 4 
	PopQueue(pq);//4
	printf("%d\n", QueueFront(pq));//4

	循环打印队列中的值
	//while (!EmptyQueue(pq))
	//{
	//	printf("%d ",QueueFront(pq));
	//	PopQueue(pq);
	//}

	DestroyQueue(pq);

}

main()
{
	test01();

	system("pause");
	return 0;
}

感谢家人的阅读,若有不准确的地方 欢迎在评论区指正!

家人们,点个请添加图片描述再走呗~

  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

D. Star.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值