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

线性表

栈&队列

二叉树

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

链队列

#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;
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是严蔚敏老师所著《数据结构》中队列的代码实现(C语言): ```c #define MAXQSIZE 100 // 最大队列长度 typedef int QElemType; // 队列元素类型 typedef struct{ QElemType *base; // 初始化的动态分配存储空间 int front; // 头指针,若队列不空,指向队列头元素 int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置 }SqQueue; // 初始化队列 Status InitQueue(SqQueue &Q){ Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType)); if(!Q.base) exit(OVERFLOW); // 存储分配失败 Q.front = Q.rear = 0; // 队头、队尾指针置为0 return OK; } // 销毁队列 Status DestroyQueue(SqQueue &Q){ free(Q.base); Q.base = NULL; // 防止产生“野指针” Q.front = Q.rear = 0; return OK; } // 清空队列 Status ClearQueue(SqQueue &Q){ Q.front = Q.rear = 0; return OK; } // 判断队列是否为空 Status QueueEmpty(SqQueue Q){ if(Q.front == Q.rear) return TRUE; else return FALSE; } // 队列长度 int QueueLength(SqQueue Q){ return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE; } // 获取队头元素 Status GetHead(SqQueue Q, QElemType &e){ if(Q.front == Q.rear) return ERROR; // 队列为空 e = Q.base[Q.front]; return OK; } // 入队 Status EnQueue(SqQueue &Q, QElemType e){ if((Q.rear + 1) % MAXQSIZE == Q.front) return ERROR; // 队列满 Q.base[Q.rear] = e; Q.rear = (Q.rear + 1) % MAXQSIZE; return OK; } // 出队 Status DeQueue(SqQueue &Q, QElemType &e){ if(Q.front == Q.rear) return ERROR; // 队列为空 e = Q.base[Q.front]; Q.front = (Q.front + 1) % MAXQSIZE; return OK; } ``` 以上就是严蔚敏老师所著《数据结构》中队列的代码实现,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Eternity_GQM

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

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

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

打赏作者

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

抵扣说明:

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

余额充值