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

    你可能感兴趣的文章
    nginx + etcd 动态负载均衡实践(二)—— 组件安装
    查看>>
    Nginx + uWSGI + Flask + Vhost
    查看>>
    Nginx Location配置总结
    查看>>
    Nginx 动静分离与负载均衡的实现
    查看>>
    Nginx 反向代理解决跨域问题
    查看>>
    Nginx 反向代理配置去除前缀
    查看>>
    nginx 后端获取真实ip
    查看>>
    Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
    查看>>
    nginx 常用配置记录
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    nginx添加模块与https支持
    查看>>
    Nginx的Rewrite正则表达式,匹配非某单词
    查看>>
    Nginx的使用总结(一)
    查看>>
    Nginx的是什么?干什么用的?
    查看>>
    Nginx访问控制_登陆权限的控制(http_auth_basic_module)
    查看>>