用带头尾指针的单链表实现队列的相关操作

前言: 在观读本篇博客之前请先阅读我之前的一篇博客顺序表的相关操作. 其中的内容可以帮你更好的理解本篇博客。

队列的实现

我这里使用带头尾指针的单链表来模拟队列的功能,采用这种方法较为方便,其入队出队操作可以分别使用单链表的尾插,头删(或者头插,尾删)来实现。先看结点和队列的结构体定义:


typedef int Qdatatype;

//结点
typedef struct Qnode {
    
    
	Qdatatype data;             
	struct Qnode* next;
}Qnode;

//队列
typedef struct Queue {
    
    
	
	//头尾指针
	struct Qnode*head;
	struct Qnode*tail;

}Queue;

初始化----创建新结点

初始化:创建一个空的队列
创建新结点:将需要插入的数据定义为“struct Qnode”的结点形式

//初始化
void Init(Queue* q)
{
    
    
	if (q == NULL)
		return;
	q->head = q->tail = NULL;

}



//创建新结点
struct Qnode* creatnode(Qdatatype val)
{
    
    
	struct Qnode* node = (struct Qnode*)malloc(sizeof(struct Qnode));
	node->data = val;
	node->next = NULL;
	return node;
}

入队-尾插

将单链表的尾结点视为队列的队尾进行插入,由于我们定义了尾结点,所以该操作的时间复杂度为O(1)。实际操作:若该队列存在且为空,将头尾指针同时指向新结点;若该队列不为空,将尾指针的next指针指向新结点,然后更新尾指针。

//入队-插入
void Queuepush(Queue* q,Qdatatype val)
{
    
    
	if (q == NULL)
		return;
	// 尾插
	struct Qnode*node = creatnode(val);
	if (q->head == NULL)
		q->head = q->tail = node;
	else
	{
    
    
		q->tail->next = node;
		q->tail = node;
	}
}

模块调试截图:
在这里插入图片描述
依次将1,2,3,4,5进行入队操作。队列有效元素:1 2 3 4 5

出队-头删

将单链表的头结点视为队首进行出队操作,时间复杂度为O(1)。具体操作:将头指针更新到下原头结点的下一个位置,同时释放原头结点。此处需要注意最后两行代码的功能:若该对俩仅有一个有效元素,则头尾指针指向同一位置,将头指针更新释放后,尾指针变成野指针,需要在此处进行处理。

//出队-头删
void Queuepop(Queue* q)
{
    
    
	if (q == NULL||q->head==NULL)
		return;
	//头删
	struct Qnode* next = q->head->next;
	free(q->head); 
	q->head = next;
	//若该队列只有一个结点,尾指针需要置空
	if (q->head == NULL)
		q->tail = NULL;
}

模块调试截图:在这里插入图片描述
对原队列(1 2 3 4 5)进行三次出队操作,队列有效元素: 4 5

获取队首/队尾元素

直接返回头指针或尾指针所指向的数据元素,但是在调用这两个函数之前需要判断队列的合法性


//获取队首元素
Qdatatype Queuefront(Queue* q)
{
    
    
	return q->head->data;
}

//队尾元素
Qdatatype Queueback(Queue* q)
{
    
    
	return q->tail->data;
}

判断队列是否为空

若队列为空,返回1;
若队列不为空,返回0.

//判断队列是否为空
int Queueempty(Queue* q)
{
    
    
	if (q->head == NULL)
		return 1;
	else
		return 0;
}

完整代码展示

//队列

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>


typedef int Qdatatype;
typedef struct Qnode {
    
    
	Qdatatype data;
	struct Qnode* next;
}Qnode;

typedef struct Queue {
    
    
	
	//头尾指针
	struct Qnode*head;
	struct Qnode*tail;

}Queue;


//初始化
void Init(Queue* q)
{
    
    
	if (q == NULL)
		return;
	q->head = q->tail = NULL;

}

//创建新结点
struct Qnode* creatnode(Qdatatype val)
{
    
    
	struct Qnode* node = (struct Qnode*)malloc(sizeof(struct Qnode));
	node->data = val;
	node->next = NULL;
	return node;
}

//入队-尾插
void Queuepush(Queue* q,Qdatatype val)
{
    
    
	if (q == NULL)
		return;
	// 尾插
	struct Qnode*node = creatnode(val);
	if (q->head == NULL)
		q->head = q->tail = node;
	else
	{
    
    
		q->tail->next = node;
		q->tail = node;
	}
}

//出队-头删
void Queuepop(Queue* q)
{
    
    
	if (q == NULL||q->head==NULL)
		return;
	//头删
	struct Qnode* next = q->head->next;
	free(q->head); 
	q->head = next;
	//若该队列只有一个结点,尾指针需要置空
	if (q->head == NULL)
		q->tail = NULL;
}

//获取队首元素
Qdatatype Queuefront(Queue* q)
{
    
    
	return q->head->data;
}

//队尾元素
Qdatatype Queueback(Queue* q)
{
    
    
	return q->tail->data;
}

//判断队列是否为空
int Queueempty(Queue* q)
{
    
    
	if (q->head == NULL)
		return 1;
	else
		return 0;
}

void test()
{
    
    
	Queue q;
	Init(&q);
	Queuepush(&q, 1);
	Queuepush(&q, 2);
	Queuepush(&q, 3);
	Queuepush(&q, 4);
	Queuepush(&q, 5);
	Queuepush(&q, 6);
	Queuepush(&q, 7);
	Queuepush(&q, 8);

	while (!Queueempty(&q))
	{
    
    
		printf("%d ", Queuefront(&q));
		Queuepop(&q);
	}
	printf("\n");

}
int main()
{
    
    
	test();
	return 0;
}

运行结果:在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43962381/article/details/111851472