第七周项目二

来源:互联网 发布:永恒之塔人物数据 编辑:程序博客网 时间:2024/06/12 20:21
  1. /*  
  2. 烟台大学计算机学院  
  3.   
  4. 文件名称:xiangmu.cpp  
  5.   
  6. 作者:刘照京  
  7.   
  8. 完成日期:2017年10月26日  
  9.   
  10. 问题描述:定义链队存储结构,实现其基本运算,并完成测试。 
  11.   
  12. 输入描述:无 
  13.   
  14. 输出描述:队列元素,出列入列元素测试结果 
  15.   
  16. */   
  17.   
  18.   
  19. LQN.h:  
  20.   
  21.   
  22. #include <stdio.h>  
  23. #include <malloc.h>  
  24. typedef int ElemType;  
  25.   
  26.   
  27. typedef struct qnode  
  28. {  
  29.     ElemType data;  
  30.   
  31.     struct qnode *next;  
  32. }DataNode;  
  33.   
  34.   
  35. typedef struct  
  36. {  
  37.     DataNode *front;  
  38.   
  39.     DataNode *rear;  
  40. }LinkQuNode;  
  41.   
  42.   
  43. void InitQueue(LinkQuNode *&q);  //初始化顺序环形队列  
  44. void DestroyQueue(LinkQuNode *&q); //销毁顺序环形队列  
  45. bool QueueEmpty(LinkQuNode *q);  //判断顺序环形队列是否为空  
  46. int QueueLength(LinkQuNode *q);   //返回队列中元素个数,也称队列长度  
  47. bool enQueue(LinkQuNode *&q,ElemType e);   //进队  
  48. bool deQueue(LinkQuNode *&q,ElemType &e);  //出队  
  49.   
  50.   
  51.   
  52. LQN.cpp:  
  53.   
  54. #include "LQN.h"  
  55.   
  56.   
  57. void InitQueue(LinkQuNode *&q)  
  58. {  
  59.     q=(LinkQuNode *)malloc(sizeof(LinkQuNode));  
  60.   
  61.     q->front=q->rear=NULL;  
  62. }  
  63.   
  64. void DestroyQueue(LinkQuNode *&q)  
  65. {  
  66.     DataNode *pre=q->front,*p;  
  67.   
  68.     if(pre!=NULL)  
  69.     {  
  70.         p=pre->next;  
  71.   
  72.         while(p!=NULL)  
  73.         {  
  74.             free(pre);  
  75.             pre=p;  
  76.             p=p->next;  
  77.         }  
  78.         free(pre);  
  79.     }  
  80.   
  81.     free(q);  
  82.   
  83. }  
  84.   
  85. bool QueueEmpty(LinkQuNode *q)  
  86. {  
  87.     return(q->rear==NULL);  
  88. }  
  89.   
  90. int QueueLength(LinkQuNode *q)  
  91. {  
  92.     int i=0;  
  93.   
  94.     DataNode *p;  
  95.   
  96.     p=q->front;  
  97.   
  98.     while(p!=NULL)  
  99.     {  
  100.         i++;  
  101.   
  102.         p=p->next;  
  103.     }  
  104.   
  105.     return i;  
  106. }  
  107. bool enQueue(LinkQuNode *&q,ElemType e)  
  108. {  
  109.     DataNode *p;  
  110.   
  111.     p=(DataNode*)malloc(sizeof(DataNode));  
  112.   
  113.     p->data=e;  
  114.   
  115.     p->next=NULL;  
  116.   
  117.     if(q->rear==NULL)  
  118.     {  
  119.         q->front=q->rear=p;  
  120.     }  
  121.     else  
  122.     {  
  123.         q->rear->next=p;  
  124.         q->rear=p;  
  125.     }  
  126. }  
  127. bool deQueue(LinkQuNode *&q,ElemType &e)  
  128. {  
  129.     DataNode *t;  
  130.   
  131.     if(q->rear==NULL)  
  132.         return false;  
  133.   
  134.     t=q->front;  
  135.   
  136.     if(q->front==q->rear)  
  137.   
  138.     q->front=q->rear=NULL;  
  139.    else  
  140.     q->front=q->front->next;  
  141.     e=t->data;  
  142.   
  143.     free(t);  
  144.   
  145.     return true;  
  146. }  
  147.   
  148.   
  149.   
  150. main:  
  151.   
  152. #include <stdio.h>  
  153. #include "LQN.h"  
  154.   
  155. int main()  
  156. {  
  157.     ElemType e;  
  158.     LinkQuNode *q;  
  159.     printf("(1)初始化链队q\n");  
  160.     InitQueue(q);  
  161.     printf("(2)依次进链队元素a,b,c\n");  
  162.     enQueue(q,'a');  
  163.     enQueue(q,'b');  
  164.     enQueue(q,'c');  
  165.     printf("(3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));  
  166.     if (deQueue(q,e)==0)  
  167.         printf("队空,不能出队\n");  
  168.     else  
  169.         printf("(4)出队一个元素%c\n",e);  
  170.     printf("(5)链队q的元素个数:%d\n",QueueLength(q));  
  171.     printf("(6)依次进链队元素d,e,f\n");  
  172.     enQueue(q,'d');  
  173.     enQueue(q,'e');  
  174.     enQueue(q,'f');  
  175.     printf("(7)链队q的元素个数:%d\n",QueueLength(q));  
  176.     printf("(8)出链队序列:");  
  177.     while (!QueueEmpty(q))  
  178.     {  
  179.         deQueue(q,e);  
  180.         printf("%c ",e);  
  181.     }  
  182.     printf("\n");  
  183.     printf("(9)释放链队\n");  
  184.     DestroyQueue(q);  
  185.     return 0;  
  186. }  



运行结果



学习心得:学会了链队环形队列的基本操作。

原创粉丝点击