数据结构:队列的实现

目录

 

一、队列

二、顺序队列的实现

三、链式队列的实现


一、队列

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

队列所需要的功能:

  • 初始化
  • 入队
  • 出队
  • 判断队列是否为空(队列是否为满)
  • 遍历栈
  • 清空队列与销毁队列

队列的分类:

  • 顺序队列:核心存储结构为数组,为连续空间。
  • 链式队列:核心存储结构为链表,为碎片空间。

  • 个人对数据结构的一点理解 

二、顺序队列的实现

  • 栈的定义
#define SIZE 10
typedef struct squeue
{
	int *data;
	int front;
	int rear;
}sq,*sq_p;
  • 初始化
/*队列初始化*/
sq_p init_squeue()
{
	sq_p squeue = malloc(sizeof(sq));
	if(squeue != NULL)
	{
		squeue->data = calloc(SIZE,sizeof(int));
		squeue->front = squeue->rear = 0;
	}

	return squeue;
}
  • 判断是否为空队(判断是否为满队)
/*判断队列是否满*/
bool isFull(sq_p squeue)
{
	return squeue->front == (squeue->rear +1)%SIZE;
}

/*判断队列是否空*/
bool isEmpty(sq_p squeue)
{
	return squeue->front == squeue->rear;
}
  • 入队
/*
	入队:在rear(尾部插入)
 */
bool in_sq(int data, sq_p squeue)
{
	//判断队列是否满了
	if (isFull(squeue))
	{
		printf("squeue is full of data\n");
		return false;
	}

	//入队
	squeue->data[squeue->rear] = data;
	squeue->rear = (squeue->rear + 1)%SIZE;//循环队列

	return true;
}
  • 出队
/*
	出队:在front(队头删除)
*/
bool out_sq(int *data,sq_p squeue)
{
	//判断对了是否为空
	if (isEmpty(squeue))
	{
		printf("squeue is empty\n");
		return false;
	}

	//出队
	*data = squeue->data[squeue->front];
	squeue->front = (squeue->front + 1)%SIZE;

	r
  • 遍历队列
/*遍历打印队列中数据*/
void showSqueue(sq_p squeue)
{
	if (isEmpty(squeue))
	{
		printf("squeue is empty\n");
		return;
	}
	//队头位置
	int pos = squeue->front;

	//遍历栈中元素
	while(pos != squeue->rear)
	{
		printf("%d\t", squeue->data[pos]);
		pos = (pos+1)%SIZE;
	}
}
  • 清空队列 与 销毁队列
/*清空队列中数据*/
void clearSqueue(sq_p squeue)
{
	if (isEmpty(squeue))
	{
		printf("squeue is empty\n");
		return;
	}

	int data;
	//清空队列中数据
	while(!isEmpty(squeue))
	{
		out_sq(&data,squeue);
	}
}
/*销毁队列*/
void destroySqueue(sq_p squeue)
{
	free(squeue->data);
	free(squeue);
}

三、链式队列的实现

  • 链式队列的定义
/*队列存储节点*/
typedef struct node
{
	int data;
	struct node *next;
}sqNode, *sqNode_p;

/*队列的队头与队尾*/
typedef struct squeue
{
	sqNode_p front;
	sqNode_p rear;	
}sqLink, *sqLink_p;
  • 初始化
/*队列初始化,包含创建存储节点的头节点*/
sqLink_p init_squeue()
{
	sqLink_p squeue = malloc(sizeof(sqLink));
	if (squeue != NULL)
	{
		//队头、队尾指向头结点
		squeue->front = squeue->rear = malloc(sizeof(sqNode));
		squeue->rear->next = NULL;//头结点的下一个节点指向NULL

		return squeue;
	}
}
  • 判断队列是否为空
/*判断队列是否为空*/
bool isEmpty(sqLink_p squeue)
{
	return squeue->front == squeue->rear;
}
  •  入队
/*入队*/
bool in_sq(int data ,sqLink_p squeue)
{
	sqNode_p new = malloc(sizeof(sqNode));
	if(new != NULL)
	{
		new->data = data;//链式结构:都优先处理新节点
		new->next = NULL;
		squeue->rear->next = new;
		squeue->rear = new;

		return true;
	}
	else return false;
}
  • 出队
/*出队*/
bool out_sq(int *data, sqLink_p squeue)
{	
	//判断队列是否为空
	if(isEmpty(squeue))
	{
		printf("squeue is empty\n");
		return false;
	}

	//squeue->front 一直指向头节点, 出队操作完成后需要更新 队尾
	sqNode_p p = squeue->front->next;//获得第一个节点
	*data = p->data;//保存数据
	squeue->front->next = p->next;//更新头节点
	if(p->next == NULL)	//只有一个节点的情况
	{
		squeue->rear = squeue->front;
	}
	free(p);

	return true;
}
  • 遍历、显示队列
/*遍历队列*/
void showSqueue(sqLink_p squeue)
{
	//判断队列是否为空
	if (isEmpty(squeue))
	{
		printf("squeue is empty\n");
		return;
	}

	sqNode_p p = squeue->front->next;//从第一个节点开始遍历
	while(p != NULL)
	{
		printf("%d\t", p->data);
		p = p->next;
	}
	printf("\n");
}
  • 清空队列 与 销毁队列
/*清空队列*/
void clearSqueue(sqLink_p squeue)
{
	//判断队列是否为空
	if (isEmpty(squeue))
	{
		printf("squeue is empty\n");
		return;
	}

	sqNode_p p = squeue->front->next;
	sqNode_p tmp;
	squeue->rear = squeue->front;//更新队列front、rear指向
	while(p != NULL)//清空数据
	{
		tmp = p->next;
		free(p);
		p = tmp;
	}
	printf("清空成功\n");
}
/*销毁队列*/
void destroySqueue(sqLink_p squeue)
{
	//判断队列是否为空
	if (isEmpty(squeue))
	{
		free(squeue);
		return;
	}

	while(squeue->front != NULL)
	{
		squeue->rear = squeue->front->next;s

		free(squeue->front);

		squeue->front = squeue->rear;
	}
	printf("销毁成功\n");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃鱼的猫丿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值