数据结构 链式队列

头文件

cpp 复制代码
#pragma once
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
using namespace std;

typedef struct listqueuehead {
	struct node* front;//头
	struct node* rear;//尾
}listqueuehead, * plistqueue;
//链式队列有效节点设计
typedef struct node {
	int data;//数据域
	struct node* next;//指针域
}node, * pnode;
//初始化
void initlistqueue(listqueuehead* p);
//入队
bool push(listqueuehead* p, int val);
//出队
bool pop(listqueuehead* p);
//判空
bool isempty(listqueuehead* p);
//清空
void  clear(listqueuehead* p);
//销毁
void destroy(listqueuehead* p);
//打印
void show(listqueuehead* p);
//获取有效值个数
int getsize(listqueuehead* p);
//获取队头元素值
int gettop(listqueuehead* p);

源文件

cpp 复制代码
#include"链式队列.h"

//初始化
void initlistqueue(listqueuehead* p) {
	assert(p != nullptr);
	p->front = p->rear = nullptr;
}
//入队
bool push(listqueuehead* p, int val) {
	assert(p != nullptr);
	node* k = (node*)malloc(sizeof(node));
	if (k == nullptr)return false;
	k->data = val;
	k->next = nullptr;
	if (isempty(p)) {
		p->front = k;
		p->rear = k;
	}
	else {
		p->rear->next = k;
		p->rear = k;
	}
	return true;
}
//出队
bool pop(listqueuehead* p) {
	assert(p != nullptr);
	if (isempty(p))return false;
	node* temp = p->front;
	if (p->front == p->rear) {
		p->front = NULL;
		p->rear = NULL;
	}
	else p->front = p->front->next;
	free(temp);
	temp = NULL;
	return true;
}
//判空
bool isempty(listqueuehead* p) {
	assert(p != nullptr);
	return p->front == nullptr;
}
//清空
void  clear(listqueuehead* p) {
	assert(p != nullptr);
	if (isempty(p))return;
	while (p->front != NULL) {
		pnode temp = p->front;
		p->front = p->front->next;
		free(temp);
		temp = NULL;
	}
	p->rear = NULL;
}
//销毁
void destroy(listqueuehead* p) {
	assert(p != nullptr);
	clear(p);
}
//打印
void show(listqueuehead* p) {
	assert(p != nullptr);
	printf("队列元素: ");
	pnode cur = p->front;
	while (cur != NULL) {
		cout << cur->data << " ";
		cur = cur->next;
	}cout << endl;
}
//获取有效值个数
int getsize(listqueuehead* p) {
	assert(p != nullptr);
	if (isempty(p))return -1;
	int count = 0;
	pnode cur = p->front;
	while (cur != nullptr) {
		count++;
		cur = cur->next;
	}
	return count;
}
//获取队头元素值
int gettop(listqueuehead* p) {
	assert(p != nullptr);
	if (isempty(p))return -1;
	return p->front->data;
}

int main() {


	return 0;
}
相关推荐
学代码的CJY1 小时前
从代码出发理解时间复杂度与空间复杂度
数据结构·算法
白帽攻防录1 小时前
SRC 挖洞:WSO2 JWT 算法混淆绕过深度复盘,CVE-2026-5430 不支持的算法怎么变成管理员
网络·算法·安全·网络安全·jwt
小O的算法实验室2 小时前
IEEE TEVC,基于精确与DQN群智能调度方法用于多目标异构USV
算法
字节跳动的猫3 小时前
LikeShop 积分体系全链路改造:获取规则、订单抵扣与积分商品兑换二开
运维·数据结构
可乐ea3 小时前
AI Agent 工具调用准确性评测:选择错误与参数错误分开测
大数据·人工智能·算法·大模型·工具调用·ai智能体·agent评测
上位机妹子3 小时前
C 语言 数组删除指定元素(快慢指针法)
c语言·数据结构·算法
心中有你02143 小时前
【路径规划】A*寻路算法最通俗易懂讲解(C语言完整实现+详细注释)
c语言·开发语言·算法
papaofdoudou4 小时前
初中数学-整数整除判定法全解:从 2 到 19 的方法、依据与证明
算法
让学习成为一种生活方式4 小时前
啤酒花基因组与重测序--Nature Communications
人工智能·算法
l1t5 小时前
DeepSeek总结的OpenZL压缩变换器
c++·算法