数据结构 链式队列

头文件

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;
}
相关推荐
JieE21216 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack201 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050732 天前
(一)小红的数组操作
算法·编程语言
怕浪猫2 天前
Electron 系列文章封面图
算法·架构·前端框架