博客
关于我
【数据结构从青铜到王者】第五篇:数据结构之队列
阅读量: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 mysql query 行数,PHP和MySQL:返回的行数
    查看>>
    PHP mysql_real_escape_string() 函数防SQL注入
    查看>>
    php mysql优化方法_MySQL优化常用方法
    查看>>
    PHP OAuth 2.0 Server
    查看>>
    php odbc驱动,php常用ODBC函数集(详细)
    查看>>
    php openssl aes ecb,php openssl_encrypt AES-128-ECB iOS
    查看>>
    php paypal rest api,PayPal REST API指定网络配置文件PHP
    查看>>
    php pcntl 多进程学习
    查看>>
    PHP pcntl_fork不能在web服务器中使用的变通方法
    查看>>
    php private ,public protected三者的区别
    查看>>
    php PSR规范
    查看>>
    php rand() 重复,array_rand()函数从另外一个数组中随机取得的一定数量的数组的元素是否会重复?...
    查看>>
    php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用
    查看>>
    php redis 集群扩展类文件
    查看>>
    php redis(2)
    查看>>
    PHP Redis分布式锁
    查看>>
    php session超时时间_php怎么设置session超时时间
    查看>>
    PHP SOAP模块的使用方法:NON-WSDL模式
    查看>>
    PHP Socket实现websocket(三)Stream函数
    查看>>
    php Socket通信
    查看>>