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

    你可能感兴趣的文章
    php 解决项目中多个自动加载冲突问题
    查看>>
    PHP 设置调试工具XDebug PHPStorm IDE
    查看>>
    php 身份证号检测
    查看>>
    PHP 输入输出流合集
    查看>>
    PHP 过滤器(Filter)
    查看>>
    php 运算符and or && || 的详解
    查看>>
    php 返回html字符串长度限制,记一次js中和php中的字符串长度计算截取的终极问题和完美...
    查看>>
    php 阿里云oss 上传回调
    查看>>
    PHP 面向对象 final类与final方法
    查看>>
    php+JQ+EasyUI自动加载数据
    查看>>
    php+sql server根据自增序号id区间查询第几条到第几条的数据
    查看>>
    php--------获取当前时间、时间戳
    查看>>
    Redis使用场景举例
    查看>>
    php--正则表达式
    查看>>
    php--防止sql注入的方法
    查看>>
    PHP-CGI Windows平台远程代码执行漏洞复现(CVE-2024-4577)
    查看>>
    php-cgi耗尽报502错误
    查看>>
    php-cgi(fpm-cgi) 进程 CPU 100% 与 file_get_content...
    查看>>
    PHP-DI/Invoker 开源项目使用教程
    查看>>
    php-fpm与Nginx运行常见错误说明
    查看>>