基础数据结构之链式队列

一、链式队列的定义

在这里插入图片描述
1)初始化
在这里插入图片描述
2)入队
在这里插入图片描述
3)出队
在这里插入图片描述

二、链式队列的代码实现

.h头文件实现

#pragma once
typedef int ELEM_TYPE;


//链式队列有效节点结构体设计
typedef struct List_Queue {
	ELEM_TYPE data;//数据域
	struct List_Queue* next;//指针域
}List_Queue,PLQueue;

//链式队列头节点结构体设计
typedef struct Node {
	/*注意一点,这里定义队头队尾指针是struct List_Queue*
	* 因为我们的头结点后面连接的是有效节点
	*/
	//队头指针
	struct List_Queue* front;
	//队尾指针
	struct List_Queue* rear;
}Node, Pnode;

//初始化
void Init_Node(struct Node* PQ);


//入队
bool Push(struct Node* PQ, ELEM_TYPE val);

//出队
bool Pop(struct Node* PQ);

//获取队头元素值
ELEM_TYPE Top(struct Node* PQ);

//搜索
struct List_Queue* search(struct Node* PQ, ELEM_TYPE val);

//判空
bool Is_Empty(struct Node* PQ);


//清空
void Clear(struct Node* PQ);

//销毁1
void Destory1(struct Node* PQ);
 
//销毁2
void Destory2(struct Node* PQ);

//获取有效值个数
int Get_length(struct Node* PQ);

//打印
void Show(struct Node* PQ);

.cpp核心代码实现

#include<stdio.h>
#include"List_Queue.h"
#include <cassert>
#include <stdlib.h>
#include<vld.h>

//初始化
void Init_Node(struct Node* PQ) {
	assert(PQ != NULL);
	PQ->front = NULL;
	PQ->rear = NULL;
}


//入队(尾插)
bool Push(struct Node* PQ, ELEM_TYPE val) {
	assert(PQ != NULL);
	struct List_Queue* Qnewnode = (struct List_Queue*)malloc(sizeof(struct List_Queue));
	assert(Qnewnode!=NULL);
	Qnewnode->data = val;

	//1.分情况讨论,进行插入,首先考虑特殊情况,空队列时,进行入队
	if (Is_Empty(PQ)) {
		Qnewnode->next = NULL;
		PQ->front = PQ->rear = Qnewnode;
		return true;
	}
	
	//2.普通情况  队列中已经有数据,插入到尾结点后面
	Qnewnode->next = PQ->rear->next;
	PQ->rear->next = Qnewnode;

	//3.尾结点的指针需要重新指向
	PQ->rear= Qnewnode;


	return true;
}

//出队
bool Pop(struct Node* PQ) {
	assert(PQ != NULL);
	if (Is_Empty(PQ)) {
		return false;
	}
	//1.用指针p指向待删除节点
	struct List_Queue* p = PQ->front;
	
	//2.分情况讨论,特殊情况当只有一个有效节点的时候
	if (p->next == NULL) {
		free(p);
		PQ->front = PQ->rear = NULL;
		return true;
	}
	//3.普通情况,跨越指向
	PQ->front = p->next;
	free(p);
	return true;
}

//获取队头元素值
ELEM_TYPE Top(struct Node* PQ) {
	assert(PQ != NULL);
	if (Is_Empty(PQ)) {
		printf("error\n");
		exit(0);
	}
	struct List_Queue* p = PQ->front;
	return p->data;
}

//搜索
struct List_Queue* search(struct Node* PQ, ELEM_TYPE val) {
	assert(PQ != NULL);
	if (Is_Empty(PQ)) {
		return NULL;
	}
	struct List_Queue* p = PQ->front;
	for (; p != NULL; p = p->next) {
		if (val == p->data) {
			return p;
		}
	}
	return NULL;
}

//判空
bool Is_Empty(struct Node* PQ) {
	assert(PQ != NULL);
	if (PQ->front==NULL) {
		return true;
	}
	return false;
}


//清空
void Clear(struct Node* PQ) {
	assert(PQ != NULL);
	Destory1(PQ);
}

//销毁
void Destory1(struct Node* PQ) {
	assert(PQ != NULL);
	while (PQ->front!=NULL)
	{
		Pop(PQ);
	}
}

void Destory2(struct Node* PQ){
	assert(PQ != NULL);
	struct List_Queue* p = PQ->front;
	struct List_Queue* q;
	PQ->front = PQ->rear = NULL;
	while (p != NULL) {
		q = p->next;
		free(p);
		p = q;
	}
}

//获取有效值个数
int Get_length(struct Node* PQ) {
	assert(PQ != NULL);
	int count = 0;
	struct List_Queue* p = PQ->front;
	for (; p != NULL; p = p->next) {
		count++;
	}
	return count;
}

//打印
void Show(struct Node* PQ) {
	assert(PQ != NULL);
	struct List_Queue* p = PQ->front;
	for (; p != NULL; p = p->next) {
		printf("%d ", p->data);
	}
	printf("\n");
}

main函数代码测试

int main() {
	struct Node head;
	Init_Node(&head);
	for (int i = 1; i <= 10; i++) {
		Push(&head,i);
	}
	Show(&head);
	Pop(&head);
	Show(&head);
	ELEM_TYPE res= Top(&head);
	printf("%d\n", res);
	//printf("f=%p,r=%p", head.front, head.rear);
	printf("%d\n", Get_length(&head));
	Destory1(&head);
	Destory2(&head);
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡蓝色的经典

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值