如何使用C语言实现队列?

1.队列的概念及结构

队列:

只允许一端插入数据,另一端删除数据的特殊线性表

先进先出FIFO(First In First Out)

入队列:

进行插入操作的一端称为队尾

出队列:

进行删除操作的一端称为队头

image-20231210103550378.png

2. 队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

image-20231216200224454.png

DFS—深度优先遍历 – 递归/栈实现非递归

BFS—广度优先遍历 – 队列

// 链式结构:表示队列
#pragma once
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>

typedef int QDatatype;
typedef struct QueueNode
{
	QDatatype data;
	struct QueueNode* next;
}QNode;


typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

//初始化队列
void QueueInit(Queue* pq);
//队尾入队列
void QueuePush(Queue* pq , QDatatype x);
//队头出队列
void QueuePop(Queue* pq);
//获取队头元素
QDatatype QueueFront(Queue* pq);
//获取队尾元素
QDatatype QueueBack(Queue* pq);
//队列大小
int QueueSize(Queue* pq);
//判断队列是否为空
bool QueueEmpty(Queue* pq);
//销毁队列
void QueueDestory(Queue* pq);
 
#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
//初始化队列
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
//销毁队列
void QueueDestory(Queue* pq)
{
	QNode* cur = pq->phead ;
	while (cur)
	{
		/*QNode* tmp = cur;
		cur = cur->next;
		free(tmp);*/
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}


//队尾入队列
void QueuePush(Queue* pq, QDatatype x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->phead == NULL)//isEmpty
	{
		assert(pq->ptail==NULL);

		pq->phead = newnode;
		pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;


}
//判断队列是否为空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->size == 0;
	//return pq->phead == NULL && pq->ptail == NULL;
}



//队头出队列
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));//assert(pq->phead != NULL);
	//1、一个节点
	//2、多个节点
	if (pq->phead->next == NULL)//一个节点要注意ptail别弄成野指针
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		Queue* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}
 
//获取队头元素
QDatatype QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->data;
}

//获取队尾元素
QDatatype QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->ptail->data;
}

//队列大小
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}
//测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"


void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);

	//QueuePop(&q);
	//QueuePop(&q);
	//QueuePop(&q);

	//while (!QueueEmpty(&q))
	//{
	//	printf("%d ", QueueFront(&q));
	//	
	//	QueuePop(&q);
	//}
	printf("\n"); 
	//printf("%d ", QueueFront(&q));
	//printf("%d ", QueueSize(&q));
	//QueuePop(&q);
	printf("%d ", QueueBack(&q));
	printf("%d ", QueueBack(&q));
	printf("%d ", QueueBack(&q));
	
	
	


	QueueDestory(&q);

}

int main()
{
	TestQueue();
	return 0;
}
  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hhh __灏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值