文章目录
一、链式队列的定义
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);
}