[数据结构]:06-队列(链表带头结点)(C语言实现)

目录

前言

已完成内容

队列实现

01-开发环境

02-文件布局

  03-代码

01-主函数

02-头文件

03-QueueCommon.cpp

04-QueueFunction.cpp

结语


前言

        此专栏包含408考研数据结构全部内容,除其中使用到C++引用外,全为C语言代码。使用C++引用主要是为了简化指针的使用,避免二重指针的出现。

已完成内容

[数据结构]:01-顺序表(C语言实现)_Chandni.的博客-CSDN博客

[数据结构]:02-单链表(C语言实现)_Chandni.的博客-CSDN博客

[数据结构]:03-栈(C语言实现)_Chandni.的博客-CSDN博客

[数据结构]:04-循环队列(数组)(C语言实现)_Chandni.的博客-CSDN博客

[数据结构]:05-循环队列(链表)(C语言实现)_Chandni.的博客-CSDN博客

队列实现

01-开发环境

        语言:C/C++14

        编译器:MinGW64

        集成开发环境:CLion2022.1.3

02-文件布局

        请在CLion集成开发环境中创建C++可执行程序,否则无法运行,原因上面已解释。

                        

  03-代码

01-主函数

        用于测试和初始化队列。

#include "./Head/QueueData.h"
#include "./Source/QueueCommon.cpp"
#include "./Source/QueueFunction.cpp"

int main() {
    LinkedQueue LQ;
    // 初始化
    InitializationQueue(LQ);
    if (JudgeQueueEmpty(LQ)) {
        printf("Empty\n");
    } else {
        printf("False\n");
    }
    printf("-----------------------\n");

    // 入队
    QueuePush(LQ, 1);
    QueuePush(LQ, 2);
    QueuePush(LQ, 3);
    QueuePrint(LQ);
    printf("-----------------------\n");

    // 出队
    ElemType value;
    QueuePop(LQ, value);
    printf("Queue Pop Value = %d\n", value);
    QueuePop(LQ, value);
    printf("Queue Pop Value = %d\n", value);
    QueuePop(LQ, value);
    printf("Queue Pop Value = %d\n", value);
    QueuePop(LQ, value); // 队列中无元素,输出Queue Empty.
    printf("-----------------------\n");

    // 入队
    QueuePush(LQ, 2);
    QueuePush(LQ, 3);
    QueuePush(LQ, 4);
    QueuePush(LQ, 5);
    QueuePush(LQ, 2);
    QueuePush(LQ, 3);
    QueuePrint(LQ);
    printf("-----------------------\n");
    return 0;
}

02-头文件

        用于存储结构体和常量等。

//
// Created by 24955 on 2023-02-26.
//

#ifndef INC_01_ARRAYQUEUE_QUEUEDATA_H
#define INC_01_ARRAYQUEUE_QUEUEDATA_H
// 头文件
#include <stdio.h>
#include <stdlib.h>

// 常量
typedef int ElemType;

// 结构体
typedef struct LinkedQueueNode {
    ElemType data;
    struct LinkedQueueNode *next;
} LinkedQueueNode;
typedef struct {
    struct LinkedQueueNode *front, *rear;
} LinkedQueue;
#endif //INC_01_ARRAYQUEUE_QUEUEDATA_H

03-QueueCommon.cpp

        用于存储公共函数以及队列的输出。

//
// Created by 24955 on 2023-02-26.
//
// 初始化队列
void InitializationQueue(LinkedQueue &LQ) {
    LQ.front = LQ.rear = (LinkedQueueNode *) malloc(sizeof(LinkedQueueNode));
    LQ.front->next = NULL;
}

// 判断队列是否为空
bool JudgeQueueEmpty(LinkedQueue LQ) {
    /*
     * 1. 头指针和尾指针相等则队列为空
     * 2. 这里的指针加引号,只是一种标识,这样说方便理解*/
    if (LQ.front == LQ.rear) {
        return true;
    } else {
        return false;
    }
}

// 输出队列元素
void QueuePrint(LinkedQueue LQ) {
    /*
     * 1. 判断队列是否为空
     * 2. 若不为空则从头输出*/
    if (!JudgeQueueEmpty(LQ)) {
        LQ.front = LQ.front->next;
        while (LQ.front) {
            printf("%3d", LQ.front->data);
            LQ.front = LQ.front->next;
        }
        printf("\n");
    } else {
        printf("Queue Empty.\n");
    }
}

04-QueueFunction.cpp

        用于存储入队、出队等函数。

//
// Created by 24955 on 2023-02-26.
//
// 入队
void QueuePush(LinkedQueue &LQ, ElemType value) {
    LinkedQueueNode *NewNode = (LinkedQueueNode *) malloc(sizeof(LinkedQueueNode));
    NewNode->data = value;
    NewNode->next = NULL;
    LQ.rear->next = NewNode;
    LQ.rear = NewNode;
}

// 出队
void QueuePop(LinkedQueue &LQ, ElemType &value) {
    /*
     * 1. 判断队列是否已空
     * 2. 若非空则出队*/
    if (!JudgeQueueEmpty(LQ)) {
        LinkedQueueNode *Node = LQ.front->next;
        value = Node->data;
        LQ.front->next = Node->next;
        // 删除最后一个元素
        if (Node == LQ.rear) {
            LQ.rear = LQ.front;
        }
        free(Node);
    } else {
        printf("Queue Empty.\n");
    }
}

结语

        本章队列实现形式为链表实现形式,队列还可使用数组实现形式,但相对较为简单,且408考试涉及概率不大(多为队列的链表实现形式),因此队列的数组实现形式本专栏不再记录。

        此博客主要用于408考研数据结构C语言实现记录,内有不足,可留言,可讨论。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Chandni.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值