数据结构 链式队列

头文件

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;
}
相关推荐
仰泳的熊猫3 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
无极低码6 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发6 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
罗超驿7 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
superior tigre7 小时前
22 括号生成
算法·深度优先
努力也学不会java8 小时前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
旖-旎8 小时前
二分查找(x的平方根)(4)
c++·算法·二分查找·力扣·双指针
ECT-OS-JiuHuaShan9 小时前
朱梁万有递归元定理,重构《易经》
算法·重构
智者知已应修善业9 小时前
【51单片机独立按键控制数码管移动反向,2片74CH573/74CH273段和位,按键按下保持原状态】2023-3-25
经验分享·笔记·单片机·嵌入式硬件·算法·51单片机