第七周项目四

来源:互联网 发布:sql二进制数据 计算符 编辑:程序博客网 时间:2024/05/18 12:30
/*
Copyright (c++) 2017,烟台大学计算机与控制工程学院
文件名称:jcy
作 者:贾存钰
完成日期:2017年10月19日
问题描述:创建10个队列,分别编号为0-9(处理为队列数组,编号即下标)。
输入若干个正整数,以数字0作为结束。设输入的值为x,其个位数字的大小为i,
则将x插入到编号为i的队列中。最后输出所有的非空队列。 
   要求将队列处理成链式队列,使用链式队列算法库中定义的数据类型及算法,
程序中只包括一个函数(main函数),入队和出队等操作直接在main函数中调用即可。 
输入描述:程序运行时输入:70 59 90 72 67 88 80 64 29 97 18 83 40 13 0 入队数值
输出描述:出队结果
*/


[cpp] view plain copy
  1. #ifndef LIQUEUE_H_INCLUDED  
  2. #define LIQUEUE_H_INCLUDED  
  3.   
  4. typedef int 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. #endif // LIQUEUE_H_INCLUDED  
[cpp] view plain copy
  1. <pre name="code" class="cpp">#include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "liqueue.h"  
  4.   
  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;   //p指向队头数据节点  
  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.     return(n);  
  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;  
  47.     if (q->rear==NULL)      //若链队为空,则新节点是队首节点又是队尾节点  
  48.         q->front=q->rear=p;  
  49.     else  
  50.     {  
  51.         q->rear->next=p;    //将*p节点链到队尾,并将rear指向它  
  52.         q->rear=p;  
  53.     }  
  54. }  
  55. bool deQueue(LiQueue *&q,ElemType &e)   //出队  
  56. {  
  57.     QNode *t;  
  58.     if (q->rear==NULL)      //队列为空  
  59.         return false;  
  60.     t=q->front;             //t指向第一个数据节点  
  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. }  
  69. </pre><br>  
  70. <br>  
  71. <pre></pre>  
  72. <p></p><pre name="code" class="cpp">#include <stdio.h>  
  73. #include <malloc.h>  
  74. #include "liqueue.h"  
  75. #define N 10  
  76. int main()  
  77. {  
  78.    int i, a;  
  79.    LiQueue *qu[N]; //定义队列指针数组  
  80.    for (i=0; i<N; i++)  
  81.     InitQueue(qu[i]);       //初始化队列  
  82.   
  83.    //为队列中加入值  
  84.   
  85.    printf("输入若干正整数,以0结束: ");  
  86.    scanf("%d", &a);  
  87.    while(a)  
  88.    {  
  89.        enQueue(qu[a%10], a);  
  90.        scanf("%d", &a);  
  91.    }  
  92.    //输出各个队列  
  93.    printf("按个位数整理到各个队列中后,各队列出队的结果是: \n");  
  94.    for (i=0; i<N; i++)  
  95.    {  
  96.        printf("qu[%d]: ", i);  
  97.        while(!QueueEmpty(qu[i]))  
  98.        {  
  99.            deQueue(qu[i], a);  
  100.            printf("%d ", a);  
  101.        }  
  102.        printf("\n");  
  103.    }  
  104.    //销毁各个队列  
  105.    for (i=0; i<N; i++)  
  106.        DestroyQueue(qu[i]);  
  107.    return 0;  
  108.   
  109. </pre><br>  
  110. <img src="http://img.blog.csdn.net/20171017213814772" alt=""><br>  
  111. <p></p>  
  112.      
原创粉丝点击