第七周项目2- 建立链队算法库
2016-10-14 10:25
246 查看
问题描述及代码: [cpp] view plain copy 1. /* 2. *烟台大学计控学院 3. *作 者:朱建豪 4. *完成日期:2016年10月14日 5. *问题描述:定义链队存储结构,实现其基本运算,并完成测试。 6. */ (1)liqueue.h [cpp] view plain copy 1. #ifndef LIQUEUE_H_INCLUDED 2. #define LIQUEUE_H_INCLUDED 3. 4. typedef char ElemType; 5. typedef struct qnode 6. { 7. ElemType data; 8. struct qnode *next; 9. } QNode; //链队数据结点类型定义 10. 11. typedef struct 12. { 13. QNode *front; 14. QNode *rear; 15. } LiQueue; //链队类型定义 16. void InitQueue(LiQueue *&q); //初始化链队 17. void DestroyQueue(LiQueue *&q); //销毁链队 18. bool QueueEmpty(LiQueue *q); //判断链队是否为空 19. int QueueLength(LiQueue *q); //返回队列中数据元素个数 20. void enQueue(LiQueue *&q,ElemType e); //入队 21. bool deQueue(LiQueue *&q,ElemType &e); //出队 22. 23. 24. 25. #endif // LIQUEUE_H_INCLUDED (2)liqueue.cpp [cpp] view plain copy 1. #include"liqueue.h" 2. #include<stdio.h> 3. #include<malloc.h> 4. void InitQueue(LiQueue *&q) //初始化链队 5. { 6. q=(LiQueue *)malloc(sizeof(LiQueue)); 7. q->front=q->rear=NULL; 8. 9. } 10. void DestroyQueue(LiQueue *&q) //销毁链队 11. { 12. QNode *p=q->front,*r; 13. if(p!=NULL) 14. { 15. r=p->next; 16. while(r!=NULL) 17. { 18. free (p); 19. p=r; 20. r=p->next; 21. } 22. } 23. free(p); 24. free(q); 25. } 26. bool QueueEmpty(LiQueue *q) //判断链队是否为空 27. { 28. return(q->rear==NULL); 29. } 30. int QueueLength(LiQueue *q) //返回队列中数据元素个数 31. { 32. int n=0; 33. QNode *p=q->front; 34. while(p!=NULL) 35. { 36. n++; 37. p=p->next; 38. 39. } 40. return (n); 41. 42. } 43. void enQueue(LiQueue *&q,ElemType e) //入队 44. { 45. QNode *p; 46. p=(QNode *)malloc(sizeof (QNode)); 47. p->data=e; 48. p->next=NULL; 49. if(q->rear==NULL) 50. q->front=q->rear=p; 51. else 52. { 53. q->rear->next=p; 54. q->rear=p; 55. 56. } 57. } 58. bool deQueue(LiQueue *&q,ElemType &e) //出队 59. { 60. QNode *t; 61. if(q->rear==NULL) 62. return false; 63. t=q->front; 64. if(q->front==q->rear) 65. q->front=q->rear=NULL; 66. else 67. q->front=q->front->next; 68. e=t->data; 69. free(t); 70. return true; 71. 72. } (3)main.cpp [cpp] view plain copy 1. #include"liqueue.h" 2. #include<stdio.h> 3. #include<malloc.h> 4. int main() 5. { 6. ElemType e; 7. LiQueue *q; 8. printf("(1)初始化链队q\n"); 9. InitQueue(q); 10. printf("(2)依次进链队元素a,b,c\n"); 11. enQueue(q,'a'); 12. enQueue(q,'b'); 13. enQueue(q,'c'); 14. printf("(3)链队为%s\n",(QueueEmpty(q)?"空":"非空")); 15. if (deQueue(q,e)==0) 16. printf("队空,不能出队\n"); 17. else 18. printf("(4)出队一个元素%c\n",e); 19. printf("(5)链队q的元素个数:%d\n",QueueLength(q)); 20. printf("(6)依次进链队元素d,e,f\n"); 21. enQueue(q,'d'); 22. enQueue(q,'e'); 23. enQueue(q,'f'); 24. printf("(7)链队q的元素个数:%d\n",QueueLength(q)); 25. printf("(8)出链队序列:"); 26. while (!QueueEmpty(q)) 27. { 28. deQueue(q,e); 29. printf("%c ",e); 30. } 31. printf("\n"); 32. printf("(9)释放链队\n"); 33. DestroyQueue(q); 34. return 0; 35. } 运行结果:
<img src="http://img.blog.csdn.net/20161014102600771?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> 知识点总结: 链队的初始化、判断是否为空、销毁、进队、出队、求链队长度 学习心得: 熟练掌握链队的基本运算
相关文章推荐
- 第七周项目2--建立链队算法库
- 第七周 项目二 建立链队算法库
- 第七周项目2-建立链队算法库
- 第七周 项目2 - 建立链队算法库
- 第七周项目2--建立链队算法库
- 第七周项目2-建立链队算法库
- 第七周-项目二 建立链队算法库
- 第七周 项目二 建立链队算法库
- 第七周 项目2 - 建立链队算法库
- 第七周 项目2 建立链队算法库
- 第七周项目2--建立链队算法库
- 第七周项目2-建立链队算法库
- 第七周数据结构之自建算法库——顺序环形队列【项目2 - 建立链队算法库】
- 第七周-项目二-建立链队算法库
- 第七周 项目2-建立链队算法库
- 第七周 项目二-建立链队算法库
- (第七周项目2)建立链队算法库
- 第七周项目2--建立链队算法库
- 第七周项目2——建立链队算法库
- 【第七周项目2-建立链队算法库】