第十六周项目4-英文单词的基数排序

来源:互联网 发布:淘宝家具运营方案 编辑:程序博客网 时间:2024/04/29 09:53
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*   
  2.  * Copyright (c) 2016, 烟台大学计算机与控制工程学院   
  3.  * All rights reserved。   
  4.  * 文件名称 :1.cpp   
  5.  * 作    者 :杨俊杰  
  6.  * 完成日期 :2016年 12月16日   
  7.  * 版 本 号 :v1.0   
  8.  * 问题描述 : 
  9.  * 输出描述 : 
  10.  */  
  11. #include <stdio.h>  
  12. #include <malloc.h>  
  13. #include <string.h>  
  14. #define MaxLen 9                //单词的最大长度  
  15. #define Radix  27               //基数rd为27,分别对应' ','a',…'z'  
  16. typedef char String[MaxLen+1];  //定义String为字符数组类型  
  17. typedef struct node  
  18. {  
  19.     String word;  
  20.     struct node *next;  
  21. } LinkNode;  
  22. void DispWord(String R[],int n) //输出单词  
  23. {  
  24.     int i;  
  25.     printf("  ");  
  26.     for (i=0; i<n; i++)  
  27.         printf("[%s] ",R[i]);  
  28.     printf("\n");  
  29. }  
  30. void PreProcess(String R[],int n)  
  31. //对单词进行预处理,用空格填充尾部至MaxLen长  
  32. {  
  33.     int i,j;  
  34.     for (i=0; i<n; i++)  
  35.     {  
  36.         if (strlen(R[i])<MaxLen)  
  37.         {  
  38.             for (j=strlen(R[i]); j<MaxLen; j++)  
  39.                 R[i][j]=' ';  
  40.             R[i][j]='\0';  
  41.         }  
  42.     }  
  43. }  
  44. void EndProcess(String R[],int n)  
  45. //恢复处理,删除预处理时填充的尾部空格  
  46. {  
  47.     int i,j;  
  48.     for (i=0; i<n; i++)  
  49.     {  
  50.         for (j=MaxLen-1; R[i][j]==' '; j--);  
  51.         R[i][j+1]='\0';  
  52.     }  
  53. }  
  54. void Distribute(String R[],LinkNode *head[],LinkNode *tail[],int j,int n)  
  55. //按关键字的第j个分量进行分配,进入此过程时各队列一定为空  
  56. {  
  57.     int i,k;  
  58.     LinkNode *p;  
  59.     for (i=0; i<n; i++)         //依次扫描R[i],将其入队  
  60.     {  
  61.         if (R[i][j]==' ')       //空格时放入0号队列中,'a'时放入1号队列中,…  
  62.             k=0;  
  63.         else  
  64.             k=R[i][j]-'a'+1;  
  65.         p=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点  
  66.         strcpy(p->word,R[i]);  
  67.         p->next=NULL;  
  68.         if (head[k]==NULL)  
  69.         {  
  70.             head[k]=p;  
  71.             tail[k]=p;  
  72.         }  
  73.         else  
  74.         {  
  75.             tail[k]->next=p;  
  76.             tail[k]=p;  
  77.         }  
  78.     }  
  79. }  
  80. void Collect(String R[],LinkNode *head[])  
  81. //依次将各非空队列中的记录收集起来  
  82. {  
  83.     int k=0,i;  
  84.     LinkNode *p;  
  85.     for (i=0; i<Radix; i++)  
  86.         for (p=head[i]; p!=NULL; p=p->next)  
  87.             strcpy(R[k++],p->word);  
  88. }  
  89. void RadixSort(String R[],int n)    //对R[0..n-1]进行基数排序  
  90. {  
  91.     LinkNode *head[Radix],*tail[Radix]; //定义Radix个队列  
  92.     int i,j;  
  93.     for (i=MaxLen-1; i>=0; i--)             //从低位到高位做d趟箱排序  
  94.     {  
  95.         for (j=0; j<Radix; j++)  
  96.             head[j]=tail[j]=NULL;           //队列置空  
  97.         Distribute(R,head,tail,i,n);        //第i趟分配  
  98.         Collect(R,head);                    //第i趟收集  
  99.     }  
  100. }  
  101. int main()  
  102. {  
  103.     int n=6;  
  104.     String R[]= {"while","if","if else","do while","for","case"};  
  105.     printf("排序前:\n");  
  106.     DispWord(R,n);  
  107.     PreProcess(R,n);  
  108.     printf("预处理后:\n");  
  109.     DispWord(R,n);  
  110.     RadixSort(R,n);  
  111.     printf("排序结果:\n");  
  112.     DispWord(R,n);  
  113.     EndProcess(R,n);  
  114.     printf("最终结果:\n");  
  115.     DispWord(R,n);  
  116.     printf("\n");  
  117.     return 0;  
  118. }  

运行结果:

0 0
原创粉丝点击