队列的链式表示和实现


  1. 队列的链式表示和实现
  2. 16年3月2日19:56:24

  3. #include <stdio.h>
  4. #include <malloc.h>
  5. #include <stdlib.h>

  6. typedef struct Qnode {
  7.     int data;
  8.     struct Qnode *pNext;
  9. }QNODE, *PQNODE;

  10. typedef struct {
  11.     PQNODE front;
  12.     PQNODE rear;
  13. }LinkQueue;

  14. /**
  15.  * Initialise the Queue. Attention : return the address of the Queue. Don't use the void .
  16.  * @param Q [the LinkQueue]
  17.  * @return [if success, return the address of the queue]
  18.  */
  19. LinkQueue * InitQueue (LinkQueue *Q)
  20. {
  21.     Q->front = Q->rear = (PQNODE)malloc(sizeof(QNODE));

  22.     if (!Q->front || !Q->rear)
  23.     {
  24.         printf("Cannot malloc memory for Q->fornt or Q->rear.\n");    
  25.         exit(-1);
  26.     }

  27.     Q->front->pNext = NULL;

  28.     return Q;
  29. }

  30. /**
  31.  * Insert the entry to the front of the queue.
  32.  * @param Q [the LinkQueue]
  33.  * @param val [the value of the entry]
  34.  * @return [if success, return 1]
  35.  */
  36. int EnQueue(LinkQueue *Q, int val)
  37. {
  38.     PQNODE pTmp;

  39.     pTmp = (PQNODE)malloc(sizeof(QNODE));
  40.     if (!pTmp)
  41.     {
  42.         printf("Can not malloc memory for pTmp.\n");    
  43.         exit(-1);
  44.     }

  45.     pTmp->data = val;
  46.     pTmp->pNext = NULL;

  47.     Q->rear->pNext = pTmp;
  48.     Q->rear = pTmp;

  49.     return 1;
  50. }

  51. /**
  52.  * Judge the LinkQueue is empty or not.
  53.  * @param Q [the LinkQueue]
  54.  * @return [if the linkqueue is empty, return 1, else return 0]
  55.  */
  56. int IsQueueEmpty(const LinkQueue *Q)
  57. {
  58.     return Q->front == Q->rear;
  59. }

  60. /**
  61.  * Remove the entry from the rear of the linkqueue.
  62.  * @param Q [the linkqueue]
  63.  * @param val [the val of the removed entry]
  64.  * @return [if success, return 1]
  65.  */
  66. int DeQueue(LinkQueue *Q, int *val)
  67. {
  68.     PQNODE pTmp;

  69.     if (IsQueueEmpty(Q))
  70.     {
  71.         printf("The Queue is empty.\n");    
  72.         exit(-1);
  73.     }

  74.     pTmp = Q->front->pNext;
  75.     *val = pTmp->data;
  76.     Q->front->pNext = pTmp->pNext;
  77.     free(pTmp);

  78.     return 1;
  79. }

  80. /**
  81.  * Ruturn the length of the linkqueue.
  82.  * @param Q [the linkqueue]
  83.  * @return [the length]
  84.  */
  85. int TheLengthOfQueue(LinkQueue Q)
  86. {
  87.     int i = 0;

  88.     PQNODE pTmp;
  89.     pTmp = Q.front;

  90.     while (pTmp != Q.rear)
  91.     {
  92.         i++;
  93.         pTmp = pTmp->pNext;
  94.     }

  95.     return i;
  96. }

  97. /**
  98.  * Traverse the linkqueue.
  99.  * @param Q [the linkqueue]
  100.  */
  101. void TraverseQueue(LinkQueue Q)
  102. {
  103.     PQNODE pTmp = Q.front->pNext;

  104.     while (pTmp)
  105.     {
  106.         printf("%d ", pTmp->data);
  107.         pTmp = pTmp->pNext;
  108.     }
  109.     printf("\n");
  110. }

  111. int main(int argc, char const *argv[])
  112. {
  113.     LinkQueue Q;
  114.     int val;

  115.     InitQueue(&Q);
  116.     EnQueue(&Q, 1);
  117.     EnQueue(&Q, 2);
  118.     EnQueue(&Q, 3);
  119.     EnQueue(&Q, 4);
  120.     TraverseQueue(Q);
  121.     printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));


  122.     if (DeQueue(&Q, &val))
  123.     {
  124.         printf("DeQueue success, the val is %d.\n", val);
  125.     }
  126.     if (DeQueue(&Q, &val))
  127.     {
  128.         printf("DeQueue success, the val is %d.\n", val);
  129.     }
  130.     printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));

  131.     return 0;
  132. }
  133. P { margin-bottom: 0.21cm; }

    1 2 3 4

    TheLengthOfQueue is 4.

    DeQueue success, the val is 1.

    DeQueue success, the val is 2.

    TheLengthOfQueue is 2.



<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(117) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值