C++ 链式队列(不带头结点)

总结归纳

  1. 对于不带头结点的队列 / 链表等数据结构,第一个插入的结点总要特殊处理,而带头结点的数据结构就没有这个问题。
  2. 对于销毁队列 DestroyQueue(LinkQueue &Q, ElemType &x) ,只需要一个循环判断:若队列一直不为空,则一直出队。其他的数据结构也是类似,通过一个递归实现。
  3. PS:返回 bool 类型的好处在这里体现了,while 循环里可以直接通过 ! 判断。
// 销毁队列
bool DestroyQueue(LinkQueue &Q, ElemType &x) {
    while (!QueueEmpty(Q)) {
        DeQuene(Q, x); // 如果队列不为空,则一直出队
    }
    return true;
}
  1. 在编写入队函数 EnQueue(LinkQueue &Q, ElemType x) 中,出现了问题,代码本身没有错,但运行时会抛出 Segmentation fault (core dumped) 错误,查询都说访问了错误的内存空间,搞来搞去不知道哪里出了问题,但申请结点后加了个判断就没问题了,搞不懂,但保险起见以后还是都这么写,申请结点后 if 判断一下:
    在这里插入图片描述

代码实现

/*
链式队列(不带头结点)
*/

#include <iostream>

using namespace std;

typedef int ElemType;

// 结点
struct LinkNode {
    ElemType data;
    LinkNode *next;
};

// 链式队列
struct LinkQueue {
    LinkNode *front, *rear; // 队头指针,队尾指针
};

// 初始化队列
void InitQueue(LinkQueue &Q) {
    Q.front = NULL;
    Q.rear = NULL;
}

// 队列判空
bool QueueEmpty(LinkQueue &Q) {
    if (Q.front == NULL) {
        return true;
    } else {
        return false;
    }
}

// 入队
bool EnQueue(LinkQueue &Q, ElemType x) {
    // LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode));
    LinkNode *s = new LinkNode;
    if (s == NULL)
        return false;
    s->data = x;
    s->next = NULL;
    if (Q.front == NULL) {
        Q.front = s;
        Q.rear = s; // 队头队尾指针指向同一位置
    } else {
        Q.rear->next = s; // 新元素插入
        Q.rear = s;       // 修改表尾指针
    }
    return true;
}

// 出队
bool DeQuene(LinkQueue &Q, ElemType &x) {
    if (Q.front == NULL) {
        return false;
    } else {
        LinkNode *p = Q.front; // 指向头结点
        x = p->data;
        if (Q.rear == p) { // 队列中只有一个元素
            Q.front = NULL;
            Q.rear = NULL;
        } else {
            Q.front = p->next; // 更新队头指针
        }
        delete p;
        return true;
    }
}

// 获取队头元素
bool GetHead(LinkQueue &Q, ElemType &head) {
    if (Q.front == NULL) {
        return false;
    } else {
        head = Q.front->data;
        return true;
    }
}

// 获取队列长度
int GetLength(LinkQueue &Q, ElemType &len) {
    if (Q.front == NULL) {
        return 0;
    } else {
        len = 1;
        LinkNode *p = Q.front; // 指向队头
        while (p != Q.rear) {
            p = p->next;
            len++;
        }
        return len;
    }
}

// 销毁队列
bool DestroyQueue(LinkQueue &Q, ElemType &x) {
    while (!QueueEmpty(Q)) {
        DeQuene(Q, x); // 如果队列不为空,则一直出队
    }
    return true;
}

int main() {
    LinkQueue Q;
    InitQueue(Q);

    ElemType head, pop, length;

    cout << "队列是否为空:" << QueueEmpty(Q) << endl;

    for (int i = 10; i < 15; i++) {
        EnQueue(Q, i);
    }

    cout << "队列是否为空:" << QueueEmpty(Q) << endl;
    GetHead(Q, head);
    cout << "队头元素:" << head << endl;
    cout << "队列长度:" << GetLength(Q, length) << endl;

    DeQuene(Q, pop);
    cout << "出队元素:" << pop << endl;
    GetHead(Q, head);
    cout << "队头元素:" << head << endl;
    cout << "队列长度:" << GetLength(Q, length) << endl;

    DestroyQueue(Q, head);
    cout << "队列是否为空:" << QueueEmpty(Q) << endl;
    cout << "队列长度:" << GetLength(Q, length) << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值