第七周--项目4队列数组

来源:互联网 发布:晋城网络 编辑:程序博客网 时间:2024/06/14 12:37
  1. *Copyright(c)2016,烟台大学计算机与控制工程学院  
  2.  *All right reserved.  
  3.  *文件名称:队列数组.cpp  
  4.  *作者:陈晓琳 
  5.  *完成日期;2016年10月14日  
  6.  *版本号;v1.0  
  7.  *  
  8.  *问题描述: 创建10个队列,分别编号为0-9(处理为队列数组,编号即下标)。输入若干个正整数,以数字0作为结束。设输入的值为x,其个位数字的大小为i,则将x插入到编号为i的队列中。最后输出所有的非空队列。   
  9.   要求将队列处理成链式队列,使用链式队列算法库中定义的数据类型及算法,程序中只包括一个函数(main函数),入队和出队等操作直接在main函数中调用即可。   
  10.   设程序运行时输入:70 59 90 72 67 88 80 64 29 97 18 83 40 13 0   
  11.   输出结果如下图:   
  12.   qu[0]:70 90 80 40  
  13.     qu[1]:   
  14.     qu[2]:72  
  15.     ......  
  16.   
  17. 提示:   
  18. - 指向单个链队的指针如下定义:   
  19.  LiQueue *qu;   
  20. - 本项目中使用的队列数组,实际上需要将十个链队的指针,顺序存储到一个数组中即可,如下定义:   
  21. LiQueue *qu[10]; //qu是数组,数组中存储指针,存储的是指向LiQueue类型的指针  
  22.   
  23.   
  24.   
  25.  *输入描述:一列数字 *程序输出:整理后的数字排列*/  
  26.   
  27.   
  28. #include <stdio.h>  
  29. #include <malloc.h>  
  30. #include "liqueue.h"  
  31. #define N 10  
  32.   
  33. int main()  
  34. {  
  35.     int i, a;  
  36.     LiQueue *qu[N]; //定义队列指针数组  
  37.     for (i=0; i<N; i++)  
  38.         InitQueue(qu[i]);       //初始化队列  
  39.   
  40.     //为队列中加入值  
  41.     printf("输入若干正整数,以0结束: ");  
  42.     scanf("%d", &a);  
  43.     while(a)  
  44.     {  
  45.         enQueue(qu[a%10], a);  
  46.         scanf("%d", &a);  
  47.     }  
  48.   
  49.     //输出各个队列  
  50.     printf("按个位数整理到各个队列中后,各队列出队的结果是: \n");  
  51.     for (i=0; i<N; i++)  
  52.     {  
  53.         printf("qu[%d]: ", i);  
  54.         while(!QueueEmpty(qu[i]))  
  55.         {  
  56.             deQueue(qu[i], a);  
  57.             printf("%d ", a);  
  58.         }  
  59.         printf("\n");  
  60.     }  
  61.   
  62.     //销毁各个队列  
  63.     for (i=0; i<N; i++)  
  64.         DestroyQueue(qu[i]);  
  65.     return 0;  
  66. }  
  67.   
  68. #include <stdio.h>  
  69. #include <malloc.h>  
  70. #include "liqueue.h"  
  71.   
  72. void InitQueue(LiQueue *&q)  //初始化链队  
  73. {  
  74.     q=(LiQueue *)malloc(sizeof(LiQueue));  
  75.     q->front=q->rear=NULL;  
  76. }  
  77. void DestroyQueue(LiQueue *&q)  //销毁链队  
  78. {  
  79.     QNode *p=q->front,*r;   //p指向队头数据节点  
  80.     if (p!=NULL)            //释放数据节点占用空间  
  81.     {  
  82.         r=p->next;  
  83.         while (r!=NULL)  
  84.         {  
  85.             free(p);  
  86.             p=r;  
  87.             r=p->next;  
  88.         }  
  89.     }  
  90.     free(p);  
  91.     free(q);                //释放链队节点占用空间  
  92. }  
  93. bool QueueEmpty(LiQueue *q)  //判断链队是否为空  
  94. {  
  95.     return(q->rear==NULL);  
  96. }  
  97. int QueueLength(LiQueue *q)  //返回队列中数据元素个数  
  98. {  
  99.     int n=0;  
  100.     QNode *p=q->front;  
  101.     while (p!=NULL)  
  102.     {  
  103.         n++;  
  104.         p=p->next;  
  105.     }  
  106.     return(n);  
  107. }  
  108. void enQueue(LiQueue *&q,ElemType e)  //入队  
  109. {  
  110.     QNode *p;  
  111.     p=(QNode *)malloc(sizeof(QNode));  
  112.     p->data=e;  
  113.     p->next=NULL;  
  114.     if (q->rear==NULL)      //若链队为空,则新节点是队首节点又是队尾节点  
  115.         q->front=q->rear=p;  
  116.     else  
  117.     {  
  118.         q->rear->next=p;    //将*p节点链到队尾,并将rear指向它  
  119.         q->rear=p;  
  120.     }  
  121. }  
  122. bool deQueue(LiQueue *&q,ElemType &e)   //出队  
  123. {  
  124.     QNode *t;  
  125.     if (q->rear==NULL)      //队列为空  
  126.         return false;  
  127.     t=q->front;             //t指向第一个数据节点  
  128.     if (q->front==q->rear)  //队列中只有一个节点时  
  129.         q->front=q->rear=NULL;  
  130.     else                    //队列中有多个节点时  
  131.         q->front=q->front->next;  
  132.     e=t->data;  
  133.     free(t);  
  134.     return true;  
  135. }  
  136.   
  137. #ifndef LIQUEUE_H_INCLUDED  
  138. #define LIQUEUE_H_INCLUDED  
  139.   
  140.   
  141. typedef int ElemType;  
  142. typedef struct qnode  
  143. {  
  144.     ElemType data;  
  145.     struct qnode *next;  
  146. } QNode;        //链队数据结点类型定义  
  147.   
  148. typedef struct  
  149. {  
  150.     QNode *front;  
  151.     QNode *rear;  
  152. } LiQueue;          //链队类型定义  
  153. void InitQueue(LiQueue *&q);  //初始化链队  
  154. void DestroyQueue(LiQueue *&q);  //销毁链队  
  155. bool QueueEmpty(LiQueue *q);  //判断链队是否为空  
  156. int QueueLength(LiQueue *q);  //返回队列中数据元素个数  
  157. void enQueue(LiQueue *&q,ElemType e);  //入队  
  158. bool deQueue(LiQueue *&q,ElemType &e);   //出队  
  159.   
  160.   
  161. #endif // LIQUEUE_H_INCLUDED  


运行结果:


知识点总结:

程序中采用顺序结构,队列数组中储存十个指向链队的指针。还要注意按位数分类时的求余,求得个位数再一一比较入队。

学习心得:

多尝试,遇到困难不能轻言放弃

0 0
原创粉丝点击