数据结构上机实践第七周项目2

来源:互联网 发布:米思米软件最新中文版 编辑:程序博客网 时间:2024/05/21 10:23


自建算法库——链队(链式队列)

实现源代码如下:

1.liqueue.h

[cpp] view plain copy
  1.   
  2.     
  3. 要求:    
  4.     1、头文件liqueue.h中定义数据结构并声明用于完成基本运算的函数。对应基本运算的函数包括:    
  5.     void InitQueue(LiQueue *&q);  //初始化链队    
  6.     void DestroyQueue(LiQueue *&q); //销毁链队    
  7.     bool QueueEmpty(LiQueue *q);  //判断链队是否为空    
  8.     int QueueLength(LiQueue *q);   //返回链队中元素个数,也称队列长度    
  9.     bool enQueue(LiQueue *&q,ElemType e);   //进队    
  10.     bool deQueue(LiQueue *&q,ElemType &e);  //出队    
  11.   2、在liqueue.cpp中实现这些函数    
  12.   3、在main函数中完成测试,包括如下内容:    
  13.     (1)初始化队列q    
  14.     (2)依次进队列元素a,b,c    
  15.     (3)判断队列是否为空    
  16.     (4)出队一个元素    
  17.     (5)输出队列中元素个数    
  18.     (6)依次进队列元素d,e,f    
  19.     (7)输出队列中元素个数    
  20.     (8)将队列中所有元素删除,并输出序列    
  21.     (9)释放队列    
  22.     
  23. *输入描述:无    
  24.     
  25. *程序输出:完成测试后的运行结果    
  26.     
  27. */      
  28. typedef char ElemType;                      //自定义字符型数据类型      
  29. typedef struct qnode                        //链队中数据节点的类型      
  30. {      
  31.     ElemType data;      
  32.     struct qnode *next;      
  33. } QNode;      
  34. typedef struct                              //链队节点的类型      
  35. {      
  36.     QNode *front;      
  37.     QNode *rear;      
  38. } LiQueue;      
  39. void InitQueue(LiQueue *&q);                //初始化链队      
  40. void DestroyQueue(LiQueue *&q);             //销毁链队      
  41. bool QueueEmpty(LiQueue *q);                //判断链队是否为空      
  42. int QueueLength(LiQueue *q);                //返回链队中元素个数,也称队列长度      
  43. void enQueue(LiQueue *&q,ElemType e);       //进队      
  44. bool deQueue(LiQueue *&q,ElemType &e);      //出队      


2.liqueue.cpp

[cpp] view plain copy
  1. #include <malloc.h>      
  2. #include "liqueue.h"      
  3. void InitQueue(LiQueue *&q)                 //初始化链队      
  4. {      
  5.     q=(LiQueue *)malloc(sizeof(LiQueue));      
  6.     q->front=q->rear=NULL;      
  7. }      
  8. void DestroyQueue(LiQueue *&q)              //销毁链队      
  9. {      
  10.     QNode *p=q->front,*r;      
  11.     if(p!=NULL)      
  12.     {      
  13.         r=p->next;      
  14.         while(r!=NULL)      
  15.         {      
  16.             free(p);      
  17.             p=r;      
  18.             r=p->next;      
  19.         }      
  20.     }      
  21.     free(p);      
  22.     free(q);      
  23. }      
  24. bool QueueEmpty(LiQueue *q)                 //判断链队是否为空      
  25. {      
  26.     return (q->rear==NULL);      
  27. }      
  28. int QueueLength(LiQueue *q)                 //返回链队中元素个数,也称队列长度      
  29. {      
  30.     QNode *p=q->front;      
  31.     int length=0;                           //设计数变量,记录表长      
  32.     while(p!=NULL)      
  33.     {      
  34.         length++;      
  35.         p=p->next;      
  36.     }      
  37.     return length;      
  38. }      
  39. void enQueue(LiQueue *&q,ElemType e)        //进队      
  40. {      
  41.     QNode *p;      
  42.     p=(QNode *)malloc(sizeof(QNode));      
  43.     p->data=e;      
  44.     p->next=NULL;                           //创建data域为e、指针域为NULL的数据节点*p      
  45.     if(q->rear==NULL)      
  46.         q->front=q->rear=p;      
  47.     else      
  48.     {      
  49.         q->rear->next=p;      
  50.         q->rear=p;      
  51.     }      
  52. }      
  53. bool deQueue(LiQueue *&q,ElemType &e)       //出队 需考虑队列为空的情况,故设置函数类型为bool型      
  54. {      
  55.     QNode *t;      
  56.     if(q->rear==NULL)      
  57.         return false;      
  58.     t=q->front;      
  59.     if(q->front==q->rear)      
  60.         q->front=q->rear=NULL;      
  61.     else      
  62.         q->front=q->front->next;      
  63.     e=t->data;      
  64.     free(t);      
  65.     return true;      
  66. }      


3.main.cpp

[cpp] view plain copy
  1. #include <stdio.h>      
  2. #include <malloc.h>      
  3. #include "liqueue.h"      
  4. int main()      
  5. {      
  6.     LiQueue *q;      
  7.     ElemType e;      
  8.       
  9.     InitQueue(q);                           //初始化队列q      
  10.     printf("该队列已被初始化!\n");      
  11.       
  12.     if(QueueEmpty(q))                       //判断队列是否为空      
  13.         printf("该队列为空\n");      
  14.     else      
  15.         printf("该队列不为空\n");      
  16.       
  17.     enQueue(q,'a');                         //依次进队列元素a,b,c      
  18.     enQueue(q,'b');      
  19.     enQueue(q,'c');      
  20.       
  21.     printf("元素a,b,c进队后,");      
  22.     if(QueueEmpty(q))                       //判断队列是否为空      
  23.         printf("该队列为空\n");      
  24.     else      
  25.         printf("该队列不为空\n");      
  26.       
  27.     if(deQueue(q,e)==0)                     //出队一个元素      
  28.         printf("此队列已为空,出队操作失败!\n");      
  29.     printf("出队成功,出队元素为%c\n",e);      
  30.       
  31.     printf("此时队列中元素个数为:%d\n",QueueLength(q));   //输出队列中元素个数      
  32.       
  33.     enQueue(q,'d');                         //依次进队列元素d,e,f      
  34.     enQueue(q,'e');      
  35.     enQueue(q,'f');      
  36.       
  37.     printf("元素d,e,f进队后,队列中元素个数为:%d\n",QueueLength(q));   //输出队列中元素个数      
  38.       
  39.     printf("出队序列为:");                 //将队列中所有元素删除,并输出序列      
  40.     while(!QueueEmpty(q))      
  41.     {      
  42.         deQueue(q,e);      
  43.         printf("%c",e);      
  44.     }      
  45.     printf("\n");      
  46.       
  47.     DestroyQueue(q);                        //释放队列      
  48.     printf("该队列已释放!\n");      
  49.       
  50.     return 0;      
  51. }      


运行结果截图如下:

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 货号查询 斐乐为什么被安踏收购 安达 安达有里 安达亚美 安达唐 安达维尔 平安达 安达祐实 安达卢西亚 安达市 安达与岛村 安达医院 源安达 安达利尔 安达曼群岛 安达易 安达信 罗安达 安达充 诺德安达 安达芬 安达站 安达曼海 奇安达 安达邮编 黑龙江安达 安达信息港 源之安达 安达里 张安达 源之安达速运单号查询 平安达腾飞单号查询 东莞安达电机有限公司 安达亚美中文字在线播放 安达铝镁加混悬液 安达卢西亚马 平安达腾飞速递单号查询 哈尔滨到安达 安达唐说明书 诺德安达双语学校