Chinaunix首页 | 论坛 | 博客
  • 博客访问: 301024
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-20 20:38
文章分类
文章存档

2016年(20)

2015年(56)

分类: 嵌入式

2016-03-02 20:02:13


  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.



阅读(1649) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~