2021-9-27【数据结构/严蔚敏】【链队列】

这篇博客详细介绍了数据结构中的线性表,包括顺序表、单链表、双向链表和静态链表的实现。接着,博主探讨了栈和队列的操作,如顺序栈、链式栈、循环队列和迷宫求解。此外,还讲解了串的存储表示方法,包括定长顺序存储和堆分配存储。最后,提供了二叉树的基本操作和遍历算法。代码实现覆盖了算法2.1至6.4,为读者提供了丰富的实践示例。

线性表

栈&队列

二叉树

持续更新中,尽请期待!!!

链队列

#include<bits/stdc++.h>
using namespace std;
#define QElemType int
#define Status int
//---------链队列实现--------------
typedef struct QNode{
    QElemType data;
    struct QNode *next;
} QNode, *QueuePtr;

typedef struct{
    QueuePtr front;
    QueuePtr rear;
} LinkQueue;
//----------基本操作----------------
Status InitQueue(LinkQueue &Q){
    Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
    if (!Q.front)
        exit(_OVERFLOW);
    Q.front->next = NULL;
    return 1;
}

Status DestroyQueue(LinkQueue &Q){
    while (Q.front){
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
    return 1;
}

Status ClearQueue(LinkQueue &Q){
    QueuePtr p, q;
    Q.rear = Q.front;
    p = Q.front->next;
    Q.front->next = NULL;
    while (p){
        q = p;
        p = p->next;
        free(q);
    }
    return 1;
}

Status QueueEmpty(LinkQueue Q){
    if (Q.front == Q.rear)
        return 1;
    else
        return 0;
}

int QueueLength(LinkQueue Q){
    int i = 0;
    QueuePtr p;
    p = Q.front;
    while (Q.rear != p){
        i++;
        p = p->next;
    }
    return i;
}

Status GetHead(LinkQueue Q, QElemType &e){
    QueuePtr p;
    if (Q.front == Q.rear)
        return 0;
    p = Q.front->next;
    e = p->data;
    return 1;
}

Status EnQueue(LinkQueue &Q, QElemType e){
    QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
    if (!p)
        exit(_OVERFLOW);
    p->data = e;
    p->next = NULL;
    Q.rear->next = p; 
    Q.rear = p;    
    return 1;
}

Status DeQueue(LinkQueue &Q, QElemType &e){
    QueuePtr p;
    if (Q.front == Q.rear)
        return 0;
    p = Q.front->next;   
    e = p->data;           
    Q.front->next = p->next; 
    if (Q.rear == p)  
        Q.rear = Q.front;
    free(p);
    return 1;
}

void visit(QElemType a){
    cout << a << " ";
}
Status QueueTraverse(LinkQueue &Q,void (*visit)(QElemType)){
    QueuePtr p;
    p = Q.front->next;
    while (p){
        visit(p->data);
        p = p->next;
    }
    cout<<endl;
    return 1;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Eternity_GQM

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

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

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

打赏作者

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

抵扣说明:

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

余额充值