数据结构上机实践第七周项目4 - 队列数组

  1. /*  
  2.   
  3. copyright (t) 2017,烟台大学计算机学院  
  4.   
  5. *All rights reserved.  
  6.   
  7. *文件名称:1.cpp  
  8.   
  9. *作者:田长航 
  10.   
  11. *完成日期:2016年10月14日  
  12.   
  13. *版本号:v1.0  
  14.   
  15. *问题描述:创建10个队列,分别编号为0-9(处理为队列数组,编号即下标)。输入若干个正整数,以数字0作为结束。设输入的值为x,其个位数字的大小为i,则将x插入到编号为i的队列中。最后输出所有的非空队列。   
  16.    要求将队列处理成链式队列,使用链式队列算法库中定义的数据类型及算法,程序中只包括一个函数(main函数),入队和出队等操作直接在main函数中调用即可。   
  17.   
  18. *输入描述:若干正整数  
  19.   
  20. *程序输出:整理后的队列  
  21.   
  22. */    
[cpp]  view plain  copy
  1.   
[cpp]  view plain  copy
  1. <pre name="code" class="cpp">//liqueue.h  
  2.   
  3. typedef int ElemType;                       //自定义整型数据类型    
  4. typedef struct qnode                        //链队中数据节点的类型    
  5. {    
  6.     ElemType data;    
  7.     struct qnode *next;    
  8. } QNode;    
  9. typedef struct                              //链队节点的类型    
  10. {    
  11.     QNode *front;    
  12.     QNode *rear;    
  13. } LiQueue;    
  14. void InitQueue(LiQueue *&q);                //初始化链队    
  15. void DestroyQueue(LiQueue *&q);             //销毁链队    
  16. bool QueueEmpty(LiQueue *q);                //判断链队是否为空    
  17. int QueueLength(LiQueue *q);                //返回链队中元素个数,也称队列长度    
  18. void enQueue(LiQueue *&q,ElemType e);       //进队    
  19. bool deQueue(LiQueue *&q,ElemType &e);      //出队    


 

[cpp]  view plain  copy
  1. //liqueue.cpp  
  2.   
  3. #include <malloc.h>    
  4. #include "liqueue.h"    
  5. void InitQueue(LiQueue *&q)                 //初始化链队    
  6. {    
  7.     q=(LiQueue *)malloc(sizeof(LiQueue));    
  8.     q->front=q->rear=NULL;    
  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.     QNode *p=q->front;    
  33.     int length=0;                           //设计数变量,记录表长    
  34.     while(p!=NULL)    
  35.     {    
  36.         length++;    
  37.         p=p->next;    
  38.     }    
  39.     return length;    
  40. }    
  41. void enQueue(LiQueue *&q,ElemType e)        //进队    
  42. {    
  43.     QNode *p;    
  44.     p=(QNode *)malloc(sizeof(QNode));    
  45.     p->data=e;    
  46.     p->next=NULL;                           //创建data域为e、指针域为NULL的数据节点*p    
  47.     if(q->rear==NULL)    
  48.         q->front=q->rear=p;    
  49.     else    
  50.     {    
  51.         q->rear->next=p;    
  52.         q->rear=p;    
  53.     }    
  54. }    
  55. bool deQueue(LiQueue *&q,ElemType &e)       //出队 需考虑队列为空的情况,故设置函数类型为bool型    
  56. {    
  57.     QNode *t;    
  58.     if(q->rear==NULL)    
  59.         return false;    
  60.     t=q->front;    
  61.     if(q->front==q->rear)    
  62.         q->front=q->rear=NULL;    
  63.     else    
  64.         q->front=q->front->next;    
  65.     e=t->data;    
  66.     free(t);    
  67.     return true;    
  68. }    

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Moresweet猫甜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值