数据结构---队列

队列: 尾进头出

先建立两个结构体

一个节点,一个队列

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;
}
相关推荐
秋夫人10 分钟前
B+树(B+TREE)索引
数据结构·算法
代码雕刻家39 分钟前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
Milo_K1 小时前
今日 leetCode 15.三数之和
算法·leetcode
Darling_001 小时前
LeetCode_sql_day28(1767.寻找没有被执行的任务对)
sql·算法·leetcode
AlexMercer10121 小时前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
Greyplayground1 小时前
【算法基础实验】图论-BellmanFord最短路径
算法·图论·最短路径
蓑 羽1 小时前
力扣438 找到字符串中所有字母异位词 Java版本
java·算法·leetcode
源代码:趴菜1 小时前
LeetCode63:不同路径II
算法·leetcode·职场和发展
儿创社ErChaungClub1 小时前
解锁编程新境界:GitHub Copilot 让效率翻倍
人工智能·算法