数据结构---队列

队列: 尾进头出

先建立两个结构体

一个节点,一个队列

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;
}
相关推荐
体系结构论文研讨会23 分钟前
多项式环及Rq的含义
算法
智驱力人工智能32 分钟前
极端高温下的智慧出行:危险检测与救援
人工智能·算法·安全·行为识别·智能巡航·高温预警·高温监测
森焱森40 分钟前
60 美元玩转 Li-Fi —— 开源 OpenVLC 平台入门(附 BeagleBone Black 驱动简单解析)
c语言·单片机·算法·架构·开源
课堂剪切板1 小时前
ch07 题解
算法·深度优先
R_AirMan2 小时前
深入浅出Redis:一文掌握Redis底层数据结构与实现原理
java·数据结构·数据库·redis
科大饭桶3 小时前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
小高Baby@3 小时前
map数据结构在Golang中是无序的,并且键值对的查找效率较高的原因
数据结构
北风toto3 小时前
python学习DataFrame数据结构
数据结构·python·学习
我爱C编程5 小时前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7895 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法