博客
关于我
【数据结构从青铜到王者】第五篇:数据结构之队列
阅读量:99 次
发布时间:2019-02-25

本文共 2440 字,大约阅读时间需要 8 分钟。

??????


??

??????????????????????????????????????????????????????????????????????????????????????????????????


??????????

1. ?????

???Queue???????????????

  • ???????????????????
  • ????????????????????
  • ????????????FIFO?First In First Out??

??????????????????????????

2. ?????

??????????????????????????????????????????????????????????????????


???????

1. ????

??????????????????????

  • ????????????

    ???????????????????????????

    typedef int QDataType;struct QueueNode {    struct QueueNode* next;    QDataType data;};
  • ????????????

    ?????????????????????????????

    struct Queue {    struct QueueNode* head;    struct QueueNode* tail;};
  • ?????

    ???????????????NULL?

    void QueueInit(struct Queue* pq) {    assert(pq);    pq->head = NULL;    pq->tail = NULL;}
  • ????

    ????????????

    void QueueDestroy(struct Queue* pq) {    assert(pq);    struct QueueNode* cur = pq->head;    while (cur) {        struct QueueNode* next = cur->next;        free(cur);        cur = next;    }    pq->head = pq->tail = NULL;}
  • ?????

    ????????????

    void QueuePush(struct Queue* pq, QDataType x) {    assert(pq);    struct QueueNode* newnode = (struct QueueNode*)malloc(sizeof(struct QueueNode));    if (newnode == NULL) {        printf("????!\n");        exit(-1);    }    newnode->data = x;    newnode->next = NULL;    if (pq->tail == NULL) {        pq->head = pq->tail = newnode;    } else {        pq->tail->next = newnode;        pq->tail = newnode;    }}
  • ?????

    ???????????

    void QueuePop(struct Queue* pq) {    assert(pq);    assert(!QueueEmpty(pq));    if (pq->head->next == NULL) {        free(pq->head);        pq->head = pq->tail = NULL;    } else {        struct QueueNode* next = pq->head->next;        free(pq->head);        pq->head = next;    }}
  • ??????

    ??????????????

    QDataType QueueFront(struct Queue* pq) {    assert(pq);    assert(!QueueEmpty(pq));    return pq->head->data;}
  • ??????

    ???????????????

    QDataType QueueBack(struct Queue* pq) {    assert(pq);    assert(!QueueEmpty(pq));    return pq->tail->data;}
  • ????????

    ???????????

    bool QueueEmpty(struct Queue* pq) {    assert(pq);    return pq->head == NULL;}
  • ??????

    ???????????

    int QueueSize(struct Queue* pq) {    int size = 0;    assert(pq);    struct QueueNode* cur = pq->head;    while (cur) {        size++;        cur = cur->next;    }    return size;}
  • ---## ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

    转载地址:http://yke.baihongyu.com/

    你可能感兴趣的文章
    Ormlite数据库
    查看>>
    orm总结
    查看>>
    os.environ 没有设置环境变量
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>
    os.system 在 Python 中不起作用
    查看>>
    OS2ATC2017:阿里研究员林昊畅谈操作系统创新与挑战
    查看>>
    OSCACHE介绍
    查看>>
    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
    查看>>
    OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
    查看>>
    SQL--mysql索引
    查看>>
    OSChina 周四乱弹 ——程序员为啥要买苹果手机啊?
    查看>>
    OSChina 技术周刊第十期,每周技术抢先看!
    查看>>
    OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called
    查看>>
    Osgi环境配置
    查看>>
    OSG中找到特定节点的方法(转)
    查看>>
    OSG学习:C#调用非托管C++方法——C++/CLI
    查看>>
    OSG学习:几何体的操作(二)——交互事件、Delaunay三角网绘制
    查看>>
    OSG学习:几何对象的绘制(三)——几何元素的存储和几何体的绘制方法
    查看>>
    OSG学习:几何对象的绘制(二)——简易房屋
    查看>>
    OSG学习:几何对象的绘制(四)——几何体的更新回调:旋转的线
    查看>>