数据结构---队列

队列:  尾进头出

先建立两个结构体

一个节点,一个队列

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

 加入函数

初始化队列

//初始化一条空列队
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;
}

最终

/*
队列
逻辑是:先进先出,后进后出   出队只能在队头,入队只能在队尾
*/
#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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值