第七周项目4-队列数组

  1. /* 
  2.  
  3. copyright (t) 2016,烟台大学计算机学院 
  4.  
  5. *All rights reserved. 
  6.  
  7. *文件名称:1.cpp 
  8.  
  9. *作者:车金阳 
  10.  
  11. *完成日期:2016年11月8日 
  12.  
  13. *版本号:v1.0 
  14.  
  15. *问题描述:创建10个队列,分别编号为0-9(处理为队列数组,编号即下标)。输入若干个正整数,以数字0作为结束。设输入的值为x,其个位数字的大小为i,则将x插入到编号为i的队列中。最后输出所有的非空队列。  
  16.    要求将队列处理成链式队列,使用链式队列算法库中定义的数据类型及算法,程序中只包括一个函数(main函数),入队和出队等操作直接在main函数中调用即可。  
  17.  
  18. *输入描述:若干正整数 
  19.  
  20. *程序输出:整理后的队列 
  21.  
  22. */  

liqueue.h:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. typedef int ElemType;                       //自定义整型数据类型  
  2. typedef struct qnode                        //链队中数据节点的类型  
  3. {  
  4.     ElemType data;  
  5.     struct qnode *next;  
  6. } QNode;  
  7. typedef struct                              //链队节点的类型  
  8. {  
  9.     QNode *front;  
  10.     QNode *rear;  
  11. } LiQueue;  
  12. void InitQueue(LiQueue *&q);                //初始化链队  
  13. void DestroyQueue(LiQueue *&q);             //销毁链队  
  14. bool QueueEmpty(LiQueue *q);                //判断链队是否为空  
  15. int QueueLength(LiQueue *q);                //返回链队中元素个数,也称队列长度  
  16. void enQueue(LiQueue *&q,ElemType e);       //进队  
  17. bool deQueue(LiQueue *&q,ElemType &e);      //出队  

liqueue.cpp:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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. }  

main.cpp:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "liqueue.h"  
  4. int main()  
  5. {  
  6.     LiQueue *qu[10];             //qu数组存指向LiQueue类型的指针  
  7.     ElemType e;  
  8.     int i,x;                     //x为输入的正整数,i为个位数字  
  9.     while(1)  
  10.     {  
  11.         for(i=0;i<10;i++)  
  12.             InitQueue(qu[i]);  
  13.         printf("输入若干正整数,以0结束:\n");  
  14.         while(scanf("%d",&x)!=EOF && x!=0)  
  15.         {  
  16.             if(x>0)  
  17.             {  
  18.                 i=x%10;          //对x除以10取余得个位数字  
  19.                 enQueue(qu[i],x);//x进队  
  20.             }  
  21.         }  
  22.         printf("按个位数整理到各个队列中后,各队列出队的结果是:\n");  
  23.         for(i=0;i<10;i++)  
  24.         {  
  25.             printf("qu[%d]:",i);  
  26.             while(!QueueEmpty(qu[i]))  
  27.             {  
  28.                 deQueue(qu[i],e);  
  29.                 printf("%d ",e);  
  30.             }  
  31.             printf("\n");  
  32.         }  
  33.         for(i=0;i<10;i++)  
  34.             DestroyQueue(qu[i]);  
  35.     }  
  36.     return 0;  
  37. }  

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值