数据结构 链式队列

头文件

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;
}
相关推荐
lolo大魔王3 分钟前
Go语言的循环语句、判断语句、通道选择语句
开发语言·算法·golang
海清河晏1114 小时前
数据结构 | 单循环链表
数据结构·算法·链表
wuweijianlove8 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
_dindong8 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志8 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
黎阳之光9 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
skywalker_119 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode
6Hzlia9 小时前
【Hot 100 刷题计划】 LeetCode 17. 电话号码的字母组合 | C++ 回溯算法经典模板
c++·算法·leetcode
wfbcg9 小时前
每日算法练习:LeetCode 209. 长度最小的子数组 ✅
算法·leetcode·职场和发展
_日拱一卒9 小时前
LeetCode:除了自身以外数组的乘积
数据结构·算法·leetcode