数据结构 链式队列

头文件

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;
}
相关推荐
sheeta19982 分钟前
LeetCode 每日一题笔记 日期:2026.05.27 题目:3121. 统计特殊字母的数量 II
笔记·算法·leetcode
ST——Jess12 分钟前
年度行业趋势研究报告:泛心理数字化赛道“流日推演”的算法困境与高保真交互范式重构
人工智能·算法·架构
Tisfy14 分钟前
LeetCode 3300.替换为数位和以后的最小元素:一次遍历
数学·算法·leetcode·模拟
garmin Chen25 分钟前
LeetcodeHot100打卡(14、合并空间,15、轮转数组,16、除了自身以外数组乘积,17.缺失的第一个整数)
java·笔记·学习·算法
elseif12343 分钟前
【C++】vector 详细版
开发语言·c++·算法
变量未定义~1 小时前
既约分数、阶乘约数、逆元、最大质因子个数【算法赛】
算法
KaMeidebaby1 小时前
卡梅德生物技术快报|Western Blot 实验应用:肺肠轴机制研究全流程技术解析
前端·数据库·人工智能·算法·百度
AhriProGramming2 小时前
计算机科普故事会-<2>见微知著
算法
BD4SXV2 小时前
线性二次调节器(Linear Quadratic Regulator,LQR)的无限时域最优控制求解与黎卡提方程
算法·自动化