专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

链式存储队列

队列的链式存储结构,简单看来和线性表的单链表非常相似,只不过,链式存储的队列,只可以在队尾rear入队,队头front出队。

同样为了方便,在队列前添加头结点。

1、1 链式存储队列结构设计:

// 结点结构
typedef struct QNode{
    QElemType data;
    struct QNode *next;
}QNode,*QListNode;
// 队列链表结构
typedef struct {
    QListNode front; // 队头
    QListNode rear;  // 队尾
}SqQueue;

1、2 初始化队列

将队列的front,rear都指向新建的头结点。

51_1.png

// 初始化
Status InitQueue(SqQueue *Q){    
    Q->front = Q->rear = (QListNode)malloc(sizeof(QListNode));
    if (!Q->front) {
        return ERROR;
    }
    Q->front->next = NULL;  // 头结点的指针域置空 此处Q->rear的next同样指向空
    return OK;
}

1、3 添加入队,只需要调整rear就可以,front始终指向头结点。

51_2.png

// 添加元素
Status EnterQueue(SqQueue *Q,QElemType e){

    if (!Q) {
        return ERROR;
    }

    QListNode temp = (QListNode)malloc(sizeof(QNode));
    if (!temp) {
        return ERROR;
    }
    temp->data = e;
    temp->next = NULL;  // 要将新结点的next指向空,以免脏数据

    Q->rear->next = temp;
    Q->rear = temp;     // 调整rear 指向最新的结点
    return OK;
}

1、4 删除出队

删除要注意两个条件:

  • 删除前判断队列是否是空的
  • 判断要删除的是不是最后一个元素

51_3.png

// 删除
Status DeleteQueue(SqQueue *Q,QElemType *e){
    if (!Q) {
        return ERROR;
    }
    if (Q->front == Q->rear) {  // 判断是否为空
        printf("队列已空\n");
        return ERROR;
    }

    QListNode temp = Q->front->next;
    *e = temp->data;
    Q->front->next = temp->next; // 要把头结点的next指向要删除结点的next

    if (Q->rear == temp) {   // 假如要删除的是最后一个,要把rear指向头结点
        Q->rear = Q->front;
    }

    free(temp);    
    return OK;
}

1、5 遍历所有元素

// 遍历
Status QueueTrave(SqQueue Q){

    QListNode temp = Q.front->next;
    while (temp) {
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("\n");

    return OK;
}

1、6 队列长度

通过遍历每一个结点,计算遍历的次数,来获取队列的长度。

获取队列长度也可以在设计队列时,添加count,每次入列出列进行count加减

// 队列长度  
int QueueLength(SqQueue Q){
    if (Q.front != Q.rear) {
        QListNode temp = Q.front->next;
        int count = 0;
        while (temp) {
            count++;
            temp = temp->next;
        }
        return count;
    }
    return 0;
}

1、7 队列清空

1、 将rear重新指向头结点
2、 遍历所有的结点,free掉
3、 将front的next指向为null

// 清空
Status ClearQueue(SqQueue *Q){
    if (!Q) {
        return ERROR;
    }

    if (Q->front == Q->rear) { // 判断队列是否为空
        return ERROR;
    }
    Q->rear = Q->front;  

    QListNode temp = Q->front->next;
    while (temp) {
        QListNode p = temp->next;
        free(temp);
        temp = p;
    }

    Q->front->next = NULL;
    return OK;
}

1、8 销毁队列

// 销毁
Status DestoryQueue(SqQueue *Q){

    QListNode temp = Q->front;
    while (temp) {
        QListNode p = temp->next;
        free(temp);
        temp = p;
    }

    /*  销毁不用在意front rear ,所以也可以不用太多其它的参数,如下
    while (Q->front) {
        Q->rear = Q->front->next;
        free(Q->front);
        Q->front = Q->rear;
    }*/

    return OK;
}

文章永久链接:https://tech.souyunku.com/31424

未经允许不得转载:搜云库技术团队 » 链式存储队列

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们