博客
关于我
【数据结构从青铜到王者】第五篇:数据结构之队列
阅读量: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/

    你可能感兴趣的文章
    python | Python高阶函数与函数式编程
    查看>>
    python | pytime,一个实用的 时间和日期处理 Python 库!
    查看>>
    python | pyupgrade,一个有趣的 Python 库!
    查看>>
    python | pyvips,一个神奇的 图像处理 Python 库
    查看>>
    Python | qutip,一个高级的 Python 库!
    查看>>
    python | rapidjson,一个实用的 提高JSON处理效率 Python 库!
    查看>>
    python | reflex,一个无敌的 Python 库!
    查看>>
    python | reloading,一个强大的 Python 库!
    查看>>
    python | rich,一个无敌的 Python 库!
    查看>>
    python | rlax,一个超强的 强化学习领域 Python 库!
    查看>>
    python | rpyc,一个超实用的 Python 库!
    查看>>
    python | rq,一个无敌的 关于Redis 的Python 库!
    查看>>
    python读取字符串指定位置字符_python要怎么截取指定位置的字符串呢?
    查看>>
    python | scikit-llm,一个神奇的 Python 库!
    查看>>
    python | sentry,一个超酷的 关于错误监控工具 Python 库!
    查看>>
    python | shiv,一个超酷的 打包工具 Python 库!
    查看>>
    python | six,一个神奇的 Python 库!
    查看>>
    Python读取图片的几种方法供net使用
    查看>>
    python | spacy,一个神奇的 Python 库!
    查看>>
    python | sqlmap,一个实用的 Python 库!
    查看>>