第九篇:基本数据结构——队列的链式表示

来源:互联网 发布:最优化方法第二版答案 编辑:程序博客网 时间:2024/04/30 05:49
该队列为链式队列,初建队列时,队头和队尾均指向头结点,头结点中不存放
数据,只存放指针,头结点的下一个节点才开始存放数据,这这样做的目的是
为了在入队和出队时方便对队列的操作,而不用考虑特殊情况。

C语言源代码

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3.   
  4. typedef struct Node  
  5. {  
  6.     int data;  
  7.     struct Node *pNext;  
  8. }NODE,*PNODE;  
  9.   
  10. typedef struct Queue  
  11. {  
  12.     PNODE front;  //队头指针  
  13.     PNODE rear;   //队尾指针  
  14. }QUEUE,*PQUEUE;  
  15.   
  16. PQUEUE create_queue();  
  17. bool is_empty(PQUEUE);  
  18. void en_queue(PQUEUE, int);  
  19. bool de_queue(PQUEUE,int *);  
  20. void destroy_queue(PQUEUE);  
  21. void traverse_queue(PQUEUE);  
  22.   
  23. int main()  
  24. {  
  25.     int data_de = 0;         //用来保存出队的元素值  
  26.   
  27.     //创建队列并进行入队测试  
  28.     PQUEUE pS = create_queue();  
  29.     en_queue(pS,2);  
  30.     en_queue(pS,4);  
  31.     en_queue(pS,6);  
  32.     traverse_queue(pS);  
  33.       
  34.     //出队测试  
  35.     if(de_queue(pS,&data_de))  
  36.         printf("delete succeed,the deleted data is: %d\n",data_de);  
  37.     else  
  38.         printf("queue is empty! delete falied!\n");  
  39.     traverse_queue(pS);  
  40.       
  41.     //销毁队列测试  
  42.     destroy_queue(pS);  
  43.     printf("queue destroyed!\n");  
  44.     traverse_queue(pS);  
  45.   
  46.     return 0;  
  47. }  
  48.   
  49. /* 
  50. 创建一个空队列,队头指针和队尾指针都指向头结点, 
  51. 头结点中不存放数据,只存放指针 
  52. */  
  53. PQUEUE create_queue()  
  54. {  
  55.     PQUEUE pS = (PQUEUE)malloc(sizeof(Queue));  
  56.     pS->front = (PNODE)malloc(sizeof(NODE));  
  57.     if(!pS || !pS->front)  
  58.     {  
  59.         printf("pS or front malloc failed!!");  
  60.         exit(-1);  
  61.     }  
  62.     else  
  63.     {  
  64.         pS->rear = pS->front;  
  65.         pS->front->pNext = NULL;  
  66.     }  
  67.     return pS;  
  68. }  
  69.   
  70. /* 
  71. 判断队列是否为空 
  72. */  
  73. bool is_empty(PQUEUE pS)  
  74. {  
  75.     if(pS->front == pS->rear)  
  76.         return true;  
  77.     else  
  78.         return false;  
  79. }  
  80.   
  81. /* 
  82. 进队函数,从队尾进队,队头指针保持不变 
  83. */  
  84. void en_queue(PQUEUE pS, int e)  
  85. {  
  86.     PNODE pNew = (PNODE)malloc(sizeof(NODE));  
  87.     if(!pNew)  
  88.     {  
  89.         printf("pNew malloc failed");  
  90.         exit(-1);  
  91.     }  
  92.     else  
  93.     {  
  94.         pNew->data = e;  
  95.         pNew->pNext = NULL;  
  96.         pS->rear->pNext = pNew;  
  97.         pS->rear = pNew;  
  98.     }  
  99.     return;  
  100. }  
  101.   
  102. /* 
  103. 出队函数,从队头出队,队尾指针保持不变,但当最后一个元素出队时, 
  104. 需要对队尾指针重新赋值,使其指向头结点 
  105. */  
  106. bool de_queue(PQUEUE pS,int *pData)  
  107. {  
  108.     if(is_empty(pS))  
  109.         return false;  
  110.     else  
  111.     {  
  112.         PNODE p = pS->front->pNext;  
  113.         *pData = p->data;  
  114.         pS->front->pNext = p->pNext;  
  115.   
  116.         //这里是队列头元素出队的特殊情况,一般情况下,删除队头元素时  
  117.         //仅需修改头结点中的指针,但当队列中最后一个元素被删除时,  
  118.         //队列尾指针也丢失了,因此需对队尾指针重新赋值(指向头结点)。  
  119.         if(pS->rear == p)           
  120.             pS->rear = pS->front;  
  121.         free(p);  
  122.     }  
  123.     return true;  
  124. }  
  125.   
  126. /* 
  127. 遍历队列,从对头向队尾依次输出队中的元素 
  128. */  
  129. void traverse_queue(PQUEUE pS)  
  130. {  
  131.     if(is_empty(pS))  
  132.         printf("there is no data in the queue!\n");  
  133.     else  
  134.     {     
  135.         PNODE pCurrent = pS->front->pNext;   
  136.         printf("Now datas int the queue are:\n");  
  137.         while(pCurrent)  
  138.         {  
  139.             printf("%d ",pCurrent->data);  
  140.             pCurrent = pCurrent->pNext;  
  141.         }  
  142.         printf("\n");  
  143.     }  
  144.     return;  
  145. }  
  146.   
  147. /* 
  148. 销毁队列,头结点也被销毁,最后也将pS节点销毁,并将其指向为空,避免垂直指针的产生 
  149. */  
  150. void destroy_queue(PQUEUE pS)  
  151. {  
  152.     if(is_empty(pS))  
  153.         return;  
  154.     else  
  155.     {  
  156.         while(pS->front)  
  157.         {  
  158.             pS->rear = pS->front->pNext;  
  159.             free(pS->front);  
  160.             pS->front = pS->rear;  
  161.         }  
  162.     }  
  163.     free(pS);  
  164.     pS = 0;  
  165.     return;  
  166. }  
原创粉丝点击