队列---链队实现

链队列详解

概述

链队列是一种基于链表实现的队列数据结构。队列是一种特殊的线性表,只允许在一端进行插入操作,在另一端进行删除操作。链队列使用一个单链表来存储队列中的元素,并使用两个指针 frontrear 来分别指示队首元素的位置和队尾元素的位置。

结构体定义

链队列的结构体定义如下:

typedef struct LinkNode {
    int data;       // 当前节点存储的数据
    struct LinkNode *next; // 指向下一个节点的指针
} LinkNode;

typedef struct LinkQueue {
    LinkNode *front; // 队首指针
    LinkNode *rear;  // 队尾指针
} LinkQueue;

基本操作

初始化队列

初始化队列时,为结构体分配内存,并设置队首和队尾指针指向同一个新创建的节点,其 next 成员为 NULL,表示队列为空:

void InitQueue(LinkQueue *&q) {
    q->front = q->rear = (LinkNode *)malloc(sizeof(LinkNode));
    q->front->next = NULL;
}

销毁队列

销毁队列时,遍历整个链表,依次释放每个节点所占用的内存:

void DestroyQueue(LinkQueue *&q) {
    LinkNode *p = q->front->next;
    LinkNode *pre = q->front;
    while (p != NULL) {
        free(pre);
        pre = p;
        p = pre->next;
    }
    free(pre);
}

判断队列是否为空

通过检查队首和队尾指针是否相等来判断队列是否为空:

bool QueueEmpty(LinkQueue *q) {
    if (q->front == q->rear) {
        return true;
    } else {
        return false;
    }
}

入队操作

向队列中添加新元素。创建一个新节点,将其数据成员设置为指定的值,并将其 next 成员设置为 NULL,然后更新队尾指针:

void enQueue(LinkQueue *&q, int e) {
    LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
    p->data = e;
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
}

出队操作

从队列中移除队首元素。如果队首和队尾指针相等,则返回 false 表示失败;否则返回队首元素,并更新队首指针:

bool deQueue(LinkQueue *&q, int &e) {
    if (q->front == q->rear) {
        return false;
    }
    LinkNode *p = q->front->next;
    e = p->data;
    q->front->next = p->next;
    if (q->rear == p) {
        q->rear = q->front;
    }
    free(p);
    return true;
}

打印队列的内容

打印队列中所有元素。如果队列为空,则输出提示信息:

void displayQueue(LinkQueue *q) {
    if (QueueEmpty(q)) {
        printf("队列中没有元素\n");
    } else {
        LinkNode *p = q->front->next;
        LinkNode *pre = q->front;
        while (p != NULL) {
            pre = p;
            p = pre->next;
            printf("%d ", pre->data);
        }
    }
    printf("\n");
}

示例代码解析

以下是一个简单的程序示例,演示了如何使用上述定义的链队列进行基本操作:

#include <stdio.h>
#include <stdlib.h>

// 定义数据结构体
typedef struct LinkNode {
    int data;
    struct LinkNode *next;
} LinkNode;

// 定义队列队头指针,队尾指针
typedef struct LinkQueue {
    LinkNode *front;
    LinkNode *rear;
} LinkQueue;

// 初始化队列
void InitQueue(LinkQueue *&q) {
    q->front = q->rear = (LinkNode *)malloc(sizeof(LinkNode));
    q->front->next = NULL;
}

// 销毁队列
void DestroyQueue(LinkQueue *&q) {
    LinkNode *p = q->front->next;
    LinkNode *pre = q->front;
    while (p != NULL) {
        free(pre);
        pre = p;
        p = pre->next;
    }
    free(pre);
}

// 判断队列是否为空
bool QueueEmpty(LinkQueue *q) {
    if (q->front == q->rear) {
        return true;
    } else {
        return false;
    }
}

// 入队
void enQueue(LinkQueue *&q, int e) {
    LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
    p->data = e;
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
}

// 出队
bool deQueue(LinkQueue *&q, int &e) {
    if (q->front == q->rear) {
        return false;
    }
    LinkNode *p = q->front->next;
    e = p->data;
    q->front->next = p->next;
    if (q->rear == p) {
        q->rear = q->front;
    }
    free(p);
    return true;
}

// 打印输出顺序队列
void displayQueue(LinkQueue *q) {
    if (QueueEmpty(q)) {
        printf("队列中没有元素\n");
    } else {
        LinkNode *p = q->front->next;
        LinkNode *pre = q->front;
        while (p != NULL) {
            pre = p;
            p = pre->next;
            printf("%d ", pre->data);
        }
    }
    printf("\n");
}

int main() {
    LinkQueue *q = (LinkQueue *)malloc(sizeof(LinkQueue));
    InitQueue(q);

    bool enFlag = true;
    while (enFlag) {
        printf("请输入需要入队的数据:");
        int e;
        scanf("%d", &e);
        enQueue(q, e);
        displayQueue(q);
        printf("入队成功\n");
        int q;
        printf("是否继续入队?(0/1):");
        scanf("%d", &q);
        enFlag = q == 1 ? true : false;
    }

    printf("入队结束\n");
    int top;
    printf("是否需要出队?(0/1)\n");
    int deFlag;
    scanf("%d", &deFlag);
    while (deFlag) {
        int e;
        deFlag = deQueue(q, e) ? 1 : 0;
        printf("出队的元素为:%d\n", e);
        displayQueue(q);
        printf("入队成功\n"); // 这里应该是 "出队成功"
        printf("是否继续出队?(0/1)\n");
        if (deFlag) {
            scanf("%d", &deFlag);
        }
    }
    printf("出队结束\n");

    printf("销毁队\n");
    DestroyQueue(q);

    return 0;
}

注意事项

  1. 内存管理:确保正确释放分配给队列的内存,避免内存泄漏。
  2. 边界条件处理:检查队列满或空的情况,避免越界访问。
  3. 输入验证:对于用户输入进行适当的验证,确保程序的健壮性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

KYGALYX

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

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

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

打赏作者

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

抵扣说明:

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

余额充值