数据结构---队列

队列: 尾进头出

先建立两个结构体

一个节点,一个队列

cpp 复制代码
typedef struct node
{
	int data;
	struct node*next;
}node,*Node;
typedef struct queue
{
	int size;//记录个数
	Node head;//队列头
	Node tail;//队列尾
}queue,Queue;

加入函数

初始化队列

cpp 复制代码
//初始化一条空列队
struct queue * init_queue(struct queue * q)
{
	//1.申请队列管理结构体的空间
	q = (struct queue *)malloc(sizeof(struct queue));
	if(q == NULL)
	{
		printf("malloc q error\n");
	}
	
	//2.给队列管理结构体赋值
	q->head = NULL;
	q->tail = NULL;
	q->size = 0;
	
	return q;
}

入列---加入节点,如果为空及头尾指针指向插入的节点

cpp 复制代码
//2.入队
int in_queue(struct queue *q,int num)
{
	//1.为新节点申请空间
	struct node *new = NULL;
	new = (struct node *)malloc(sizeof(struct node));
	if(new == NULL)
	{
		printf("malloc new error\n");
		return -1;
	}
	
	//2.赋值
	new->data = num;
	new->next = NULL;//新排队的节点后面百分百没有节点的
	
	if(q->size == 0)//如果插入的节点是第一个节点
	{
		//如果队列中只有你一个,你又是队头又是队尾
		q->head = new;
		q->tail = new;
	}
	else{	//原本 q->tail代表就是最后一个节点 q->tail->next = NULL
		q->tail->next = new;
		q->tail = new;
	}
	q->size++;
	
	return 0;
}

出列---

cpp 复制代码
int out_queue(struct queue *q,int *p)
{
	//出队前先判断是不是空队列
	if(q->size == 0)
	{
		return -1;
	}
	
	//2.不为空则正常出队
	struct node *tmp = q->head;		//tmp就是等下要出队的那个节点
	
	if(q->size == 1)//整个队列就剩一个节点
	{
		q->head = NULL;
		q->tail = NULL;
	}
	else{
		q->head = q->head->next;
	}
	
	*p = tmp->data;
	free(tmp);
	q->size--;
	
	return 0;
}

遍历

cpp 复制代码
//遍历队列中所有的元素
int show_queue(struct queue *q)
{
	//1.判断是不是空队列
	if(q->size == 0)
	{
		return -1;
	}
	
	//2.遍历队列
	struct node *tmp = NULL;
	for(tmp = q->head;tmp!=NULL;tmp=tmp->next)
	{
		printf("%d ",tmp->data);
	}
	printf("\n");
	
	return 0;
}

最终

cpp 复制代码
/*
队列
逻辑是:先进先出,后进后出   出队只能在队头,入队只能在队尾
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

//队列节点结构体
struct node {
	int data;
	struct node * next;	//指向队列中下一个节点的指针
};

struct queue{
	struct node *head;		//指向队头的指针
	struct node *tail;		//指向队尾的指针
	int size;				//统计队列中节点的个数
};

//初始化一条空列队
struct queue * init_queue(struct queue * q)
{
	//1.申请队列管理结构体的空间
	q = (struct queue *)malloc(sizeof(struct queue));
	if(q == NULL)
	{
		printf("malloc q error\n");
	}
	
	//2.给队列管理结构体赋值
	q->head = NULL;
	q->tail = NULL;
	q->size = 0;
	
	return q;
}

//2.入队
int in_queue(struct queue *q,int num)
{
	//1.为新节点申请空间
	struct node *new = NULL;
	new = (struct node *)malloc(sizeof(struct node));
	if(new == NULL)
	{
		printf("malloc new error\n");
		return -1;
	}
	
	//2.赋值
	new->data = num;
	new->next = NULL;//新排队的节点后面百分百没有节点的
	
	if(q->size == 0)//如果插入的节点是第一个节点
	{
		//如果队列中只有你一个,你又是队头又是队尾
		q->head = new;
		q->tail = new;
	}
	else{	//原本 q->tail代表就是最后一个节点 q->tail->next = NULL
		q->tail->next = new;
		q->tail = new;
	}
	q->size++;
	
	return 0;
}

int out_queue(struct queue *q,int *p)
{
	//出队前先判断是不是空队列
	if(q->size == 0)
	{
		return -1;
	}
	
	//2.不为空则正常出队
	struct node *tmp = q->head;		//tmp就是等下要出队的那个节点
	
	if(q->size == 1)//整个队列就剩一个节点
	{
		q->head = NULL;
		q->tail = NULL;
	}
	else{
		q->head = q->head->next;
	}
	
	*p = tmp->data;
	free(tmp);
	q->size--;
	
	return 0;
}

//遍历队列中所有的元素
int show_queue(struct queue *q)
{
	//1.判断是不是空队列
	if(q->size == 0)
	{
		return -1;
	}
	
	//2.遍历队列
	struct node *tmp = NULL;
	for(tmp = q->head;tmp!=NULL;tmp=tmp->next)
	{
		printf("%d ",tmp->data);
	}
	printf("\n");
	
	return 0;
}




int main()
{
	//1.初始化一条空的队列
	struct queue * q = init_queue(q);
	
	//2.入队
	in_queue(q,10);
	in_queue(q,20);
	in_queue(q,30);

	show_queue(q);
	
	//4.出队
	int a;
	while(q->size != 0)
	{
		out_queue(q,&a);
		printf("a = %d\n",a);
		printf("=============\n");
		show_queue(q);
	}
	
	free(q);
	return 0;
}
相关推荐
董董灿是个攻城狮1 天前
AI视觉连载8:传统 CV 之边缘检测
算法
AI软著研究员1 天前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish1 天前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱1 天前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
地平线开发者2 天前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮2 天前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者2 天前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考2 天前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx2 天前
CART决策树基本原理
算法·机器学习
Wect2 天前
LeetCode 210. 课程表 II 题解:Kahn算法+DFS 双解法精讲
前端·算法·typescript