数据结构--队列

队列是什么

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

我们可以画图来看看
在这里插入图片描述

队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

这里是因为,我们用数组来设计队列的话,在队头上出数据,就会涉及到数组内的所有元素的整体迁移,还有对于空间的利用,用链表,可以根据想增加的节点来增加空间,而数组就不能这么方便了,一次扩容,假如剩下的元素就那么几个,就会极大的浪费空间。
链表实现的队列则有以下优势:

1.动态大小:链表队列可以根据需要动态地分配内存,不需要预设队列的大小,因此在空间利用率上更加灵活。
2.插入和删除效率:在链表队列中,入队(enqueue)操作是在链表尾部添加一个节点,出队(dequeue)操作是删除链表头部的节点。这些操作在理想情况下时间复杂度是O(1),即便是在最坏的情况下,这个时间复杂度也是常数级别的。
3.无需整体迁移:与数组队列相比,链表队列在进行入队和出队操作时,不需要像数组那样整体迁移元素,因此在添加或移除元素时效率更高。
4.内存利用:链表队列不需要连续的内存空间,可以更好地利用计算机的内存碎片。

下面我们来设计我们的队列

typedef int datatype;
struct queue_node
{
	datatype data;
	struct queue_node* next;
};
typedef struct queue_node queue_node;
struct queue_point
{
	struct queue_node* phead;
	struct queue_node* ptail;
	int size;
};
typedef struct queue_point queue_point;

这里我们用了两个结构体,第一个结构体用来表示队列中1的每个节点,第二个就是用来表示队列的队头和队尾的指针,为什么这里要单独设计一个结构体,这样的目的是为了避免在函数传参的时候传二级指针,我们单独设计一个结构体,对这个结构体解引用,访问,就能访问这结构体里面phead,ptail这两个指针原始指向的位置,而如果要访问指针原始指向的位置,采用函数传参的话,就要传二级指针。这样会导致函数的接口参数不一致size代表,队列里面存放的元素个数。

队列初始化

void queueinit(queue_point* point)//初始化
{
	point->phead = NULL;
	point->ptail = NULL;
	point->size = 0;
}
void init_node(queue_node* node)
{
	node->data = 0;
	node->next = NULL;
}

这里初始化就把我们创建的结构体里面的指针变量置为空,把相应的数据置为0,这样是避免野指针的出现。

尾插入,入队

void insert_push(queue_point* point, datatype data)//尾插入
{
	assert(point);
	queue_node* newnode = NULL;
	newnode = (queue_node*)malloc(sizeof(queue_node));
	if (newnode == NULL)
	{
		perror("newnode_fail");
		exit(1);
	}
	newnode->data = data;
	newnode->next = NULL;
	if (point->ptail == NULL)
	{
		point->ptail = point->phead = newnode;
		point->size++;
	}
	else
	{
		point->ptail->next = newnode;
		point->ptail = newnode;
		point->size++;
	}
}

我们画图来解释一下
在这里插入图片描述
在这里插入图片描述

删除队头

这里同样有两种情况需要判断,一种是当队列里面只有1个元素的时候,直接释放就行了,还有一种就是,队列有多个元素,这时候我们需要用一个temp变量来存放队头的下一个节点,在来释放队头,在让下一个节点变成新的队头。

void del_push_data(queue_point* point)//删除对头
{
	assert(point && point->size != 0);
	queue_node* temp = NULL;
	if (point->phead->next == NULL)
	{
		free(point->phead);
		point->phead = NULL;
		point->ptail = NULL;
		point->size = 0;
	}
	else
	{
		temp = point->phead->next;
		free(point->phead);
		point->phead = temp;
		point->size--;
	}
}

在这里插入图片描述
在这里插入图片描述

删除队列

void free_queue(queue_point* point)//删除队列
{
	queue_node* prev = NULL;
	queue_node* pcur = point->phead;
	while (pcur)
	{
		prev = pcur;
		pcur = pcur->next;
		free(prev);
	}
	point->phead = point->ptail = NULL;
	point->size = 0;
}

这里就是循环删除队列。

获得队头数据

datatype get_front_data(queue_point* point)//取队头数据
{
	assert(point && point->size > 0);
	assert(point->phead);
	return point->phead->data;
}

直接返回头节点的元素就行了。

获得队尾数据

datatype get_back_data(queue_point* point)//取出队尾数据
{
	assert(point && point->size > 0);
	assert(point->phead);
	return point->ptail->data;
}

同理队尾也是一样的。

判空

bool queue_empty(queue_point* point)//判空
{
	assert(point);
	if (point->size == 0)
		return true;
	else
		return false;
}

判空直接用size来判断,当size等于0时,就代表队列里面没有数据了。

源码

main.c

# include"head.h"

void init_node(queue_node* node)
{
	node->data = 0;
	node->next = NULL;
}
void queueinit(queue_point* point)//初始化
{
	point->phead = NULL;
	point->ptail = NULL;
	point->size = 0;
}
void insert_push(queue_point* point, datatype data)//尾插入
{
	assert(point);
	queue_node* newnode = NULL;
	newnode = (queue_node*)malloc(sizeof(queue_node));
	if (newnode == NULL)
	{
		perror("newnode_fail");
		exit(1);
	}
	newnode->data = data;
	newnode->next = NULL;
	if (point->ptail == NULL)
	{
		point->ptail = point->phead = newnode;
		point->size++;
	}
	else
	{
		point->ptail->next = newnode;
		point->ptail = newnode;
		point->size++;
	}
}
void del_push_data(queue_point* point)//删除对头
{
	assert(point && point->size != 0);
	queue_node* temp = NULL;
	if (point->phead->next == NULL)
	{
		free(point->phead);
		point->phead = NULL;
		point->ptail = NULL;
		point->size = 0;
	}
	else
	{
		temp = point->phead->next;
		free(point->phead);
		point->phead = temp;
		point->size--;
	}
}
void print_queue(queue_point* point)//打印队列
{
	assert(point && point->size > 0);
	queue_node* pcur = point->phead;
	while (pcur)
	{
		printf("%d ->", pcur->data);
		pcur = pcur->next;
	}
	printf("NULL");
}
void free_queue(queue_point* point)//删除队列
{
	queue_node* prev = NULL;
	queue_node* pcur = point->phead;
	while (pcur)
	{
		prev = pcur;
		pcur = pcur->next;
		free(prev);
	}
	point->phead = point->ptail = NULL;
	point->size = 0;
}
datatype get_front_data(queue_point* point)//取对头数据
{
	assert(point && point->size > 0);
	assert(point->phead);
	return point->phead->data;
}
datatype get_back_data(queue_point* point)//取出队尾数据
{
	assert(point && point->size > 0);
	assert(point->phead);
	return point->ptail->data;
}
int get_size(queue_point* point)//获得大小
{
	assert(point->phead && point->size > 0);
	return  point->size;
}

bool queue_empty(queue_point* point)//判空
{
	assert(point);
	if (point->size == 0)
		return true;
	else
		return false;
}

test.c

# include"head.h"
void test()
{
	queue_point point;
	queue_node node;
	//init_node(&node);
	queueinit(&point);
	insert_push(&point, 1);
	insert_push(&point, 2);
	del_push_data(&point);
	print_queue(&point);
}

int main()
{
	test();
	return 0;
}

head.h

#pragma once
# include<stdio.h>
# include<stdlib.h>
# include<assert.h>
# include<stdbool.h>
typedef int datatype;
struct queue_node
{
	datatype data;
	struct queue_node* next;
};
typedef struct queue_node queue_node;
struct queue_point
{
	struct queue_node* phead;
	struct queue_node* ptail;
	int size;
};
typedef struct queue_point queue_point;

void queueinit(queue_point* p);
void init_node(queue_node* node);
//初始化
void insert_push(queue_point* point, datatype data);
//尾插
void insert_push(queue_point* point, datatype data);

void del_push_data(queue_point* point);//删除对头

void free_queue(queue_point* point);//删除队列

void print_queue(queue_point* point);//打印队列

datatype get_front_data(queue_point* point);//取对头数据;

datatype get_back_data(queue_point* point);//取出队尾数据

int get_size(queue_point* point);//获得数量

bool queue_empty(queue_point* point);//判空

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值