队列的实现(单链表)

目录

一、queue.h(头文件)

二、queue.c(调用函数)

三、test.c(主程序)

一、queue.h(头文件)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef int QDataType;
typedef struct Queuenode
{
	QDataType a;
	struct Queuenode* next;
}Qnode;

typedef struct Queue
{
	Qnode* tail;
	Qnode* head;
	int size;
}Queue;

//初始化队列
void QueueInit(Queue* ps);
//销毁队列
void QueueDestroy(Queue* ps);
//入队列
void Queuepush(Queue* ps, QDataType x);
//出队列
void Queuepop(Queue* ps);
//查看队列中的元素个数
int Queuesize(Queue* ps);
//判断队列是否为空
bool QueueEmpty(Queue* ps);
//查看队首
QDataType QueueFront(Queue* ps);
//查看队尾
QDataType QueueBack(Queue* ps);

二、queue.c(调用函数)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"

//创建新节点
Qnode* Creatnode(QDataType x)
{
	Qnode* node = (Qnode*)malloc(sizeof(Qnode));
	if (node == NULL)
	{
		perror("malloc:");
		return NULL;
	}
	node->a = x;
	node->next = NULL;
	return node;
}

//初始化
void QueueInit(Queue* ps)
{
	assert(ps);

	ps->head = NULL;
	ps->size = 0;
	ps->tail = NULL;
}

//判断队列是否为空
bool QueueEmpty(Queue* ps)
{
	assert(ps);

	return ps->size == 0;
}

//销毁队列
void QueueDestroy(Queue* ps)
{
	assert(ps);
	Qnode* cur = ps->head;
	while (cur)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}

	ps->head = ps->tail = NULL;
	ps->size = 0;
}

//入队列
void Queuepush(Queue* ps, QDataType x)
{
	assert(ps);

	Qnode* newnode = Creatnode(x);
	if (ps->head == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	ps->size++;
}

//出队列
void Queuepop(Queue* ps)
{
	assert(ps);
	//断言看ps里面是否有数据,非空执行,空的话报错
	assert(!QueueEmpty(ps));

	if (ps->head == ps->tail)
	{
		free(ps->head);
		ps->head = ps->tail = NULL;
		ps->size--;
	}
	else
	{
		Qnode* cur = ps->head;
		ps->head = ps->head->next;
		free(cur);
		cur = NULL;
		ps->size--;
	}
}
//查看队列中的元素个数
int Queuesize(Queue* ps)
{
	assert(ps);

	return ps->size;
}

//查看队首
QDataType QueueFront(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->head->a;
}
//查看队尾
QDataType QueueBack(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));

	return ps->tail->a;
}

三、test.c(主程序)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"

void Print(Queue* ps)
{
	printf("队头数据为->%d\n", QueueFront(ps));
	printf("队尾数据为->%d\n", QueueBack(ps));
}
int main()
{
	//创建队列
	Queue queue;

	//初始化
	QueueInit(&queue);

	//入队列
	Queuepush(&queue, 1);
	Queuepush(&queue, 2);
	Queuepush(&queue, 3);
	Queuepush(&queue, 4);
	Print(&queue);

	//出队列
	Queuepop(&queue);
	//查看队列中的元素个数
	int n = Queuesize(&queue);
	printf("%d\n", n);
	Print(&queue);

	Queuepush(&queue, 5);
	n = Queuesize(&queue);
	printf("%d\n", n);
	Print(&queue);

	//销毁队列
	QueueDestroy(&queue);
	return 0;
}
相关推荐
聚客AI15 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v18 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工20 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农21 小时前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了21 小时前
AcWing学习——双指针算法
c++·算法
moonlifesudo1 天前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法