标题:排序的程序,麻烦大家看看
只看楼主
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
 问题点数:0 回复次数:1 
排序的程序,麻烦大家看看
基数排序
    using System;
    using System.Text;
    using System.Collections;
    using
    using System.Data;

    namespace d
    {
   
        public class ListNode    //结点类
        {
            public string[]keys;   //关键字数组
            public ListNode link;  //指针
            public  int Length;    //关键字个数
            public ListNode()      //构造函数
            {
                link=null;
                keys=new string[10];
                Length=10;
                for(int i=0;i<10;i++)
                    keys[i]="";
            }

            public ListNode(string[]key,int len)  //带参数构造函数,构造一个带len个关键字的记录
            {
                int i=0;
                Length=len;
                keys=new string[len];
                foreach(string c in key)  
                {
                    keys[i]=c;
                    i++;
                }
                link=null;
            }

        }


        public class List   //链表类
        {
            private ListNode Head;   //头指针
            private ListNode Tail;   //尾指针
            private ListNode Current; //当前指针
            private int index;      

            public List()         //构造函数
            {
                Head=null;
                Tail=null;
                index=0;
            }

            /// <summary>
            /// 尾部添加数据
            /// </summary>

            public void Append(string [] key,int n )    //向链表插入一个新的结点
            {
                ListNode NewNode=new ListNode(key,n );
                if (index==0)  //如果头指针为空
                {
                    Head=NewNode;
                    Tail=NewNode;
                }
                else
                {
                    Tail.link =NewNode;
                    Tail=NewNode;
                }
                Current=NewNode;
                index+=1;//链表数据个数加一
            }
        
            public void print(int Len)         //打印函数
            {
                ListNode p=Head;
                int j=0;
                while(p!=null)
                {
                    Console.Write("第{0}条记录  :",(j++).ToString());
                    for(int i=0;i<Len;i++)
                        Console.Write("key[{0}]={1}  ",i.ToString(),p.keys[i]);
                    Console.WriteLine();
                    p=p.link;
                }
            }

            public  void RadixSort(int d,int radix)   //基数排序
            {
                ListNode[] front= new ListNode[radix]; //radix个队列的头指针
                ListNode []rear=new ListNode[radix];   //radix个队列的尾指针
                for(int  j=0;j<radix;j++)              //初始化
                {
                    front[j]=Tail.link  ;
                    rear[j]=Tail.link ;
                }

                ListNode current=Head;
         
                for(int i=d-1;i>=0;i--)   //将关键码keys[i]排序,i=d-1,d-2,..,0
                {
                    for(int  j2=0;j2<radix;j2++)      //将各箱初始化为空队列
                        front[j2]=null;
                    Console.WriteLine("第{0}趟排序:",i.ToString());   
                    while(current!=null)    //将n个对象分配到radix个队列中去
                    {
                        int k=Convert.ToInt32(current.keys[i]);  //取当前检测对象的第i个关键码
                        if(front[k]==null)      //第k个队列空,该对象成为队头
                            front[k]  =current;
                        else                    //不空,队尾结点链域指向它
                            rear[k].link=current;
                        rear[k] =current;        //该对象成为新的队尾
                        current=current.link ;
                    }

                    
                    int j=0;        //依次从各个队列中把对象取出并拉链

                    while(front[j] ==null) j++;   //找到第一个非空队列

                    Head=current=front[j];
                    ListNode last=rear[j];
                    for(int k=j+1;k<radix;k++)     //连接其余的队列
                        if(front[k] !=null)
                        {
                            last.link=front[k] ;
                            last=rear[k];
                            ListNode p1=front[k];
                        }
                    last.link=null;   //新链表表尾

                    for(int k=0;k<radix;k++)     //输出第d个关键排序后的链表
                        if(front[k] !=null)
                        {
                            ListNode p1=front[k];
                            Console.WriteLine("k={0}",k);
                            while(p1!=rear[k])
                                {
                                    for(int k1=0;k1<d;k1++)
                                        Console.Write("keys[{0}]={1};",k1,p1.keys[k1]);
                                    Console.WriteLine();
                                    p1=p1.link;
                                }
                            for(int k1=0;k1<d;k1++)
                                Console.Write("keys[{0}]={1};",k1,p1.keys[k1]);
                            Console.WriteLine();

                        }
                }
            }

            public void Write(int len)   //将含有len个关键码的记录写入文本文件中
            {
                ListNode p=Head;
                StreamWriter sW=new StreamWriter("d:\\ab.txt");
                int j=len-1;
                while(p!=null)
                {
                    string s="";
                    for(int i=0;i<j;i++)
                    {
                        s+=p.keys[i];
                        s+=",";
                    }
                    s+=p.keys[j];
                    sW.WriteLine(s);
                    p=p.link;
                }
                sW.Close();
            }
        }

        class sort
        {
        
            static void Main(string []args)
            {
                StreamReader sr = new StreamReader("d:\\abc.txt");
                string line = sr.ReadLine();
                string info = "";
                int len=0;
               
                if(line!=null)
                {
                    string []s1=line.Split(new char[]{','});
                    foreach(string c in s1)
                        len++;
                }
                else
                {
                    Console.WriteLine("文件为空");
                    return;
                }

                List p=new List();
                while(line != null)
                {
                    info += line;
                    line = sr.ReadLine();
                    string[] s = info.Split(new char[]{','});
                    p.Append(s,len);
                    info="";
                }
                Console.WriteLine("初始化链表:");
                p.print(len);
                p.RadixSort(len,10);
                Console.WriteLine("排序后的链表:");
                p.print(len);
                p.Write(len);
            }
        }

    }
我的目的是排列字符串的顺序,如
ADFDSABNKDJDKHIGF
AKJDFIDSJIFS
KJDISFSA
BDJ
GBDFJ

排序后应该是

ADFDSABNKDJDKHIGF
AKJDFIDSJIFS
BDJ
GBDFJ
KJDISFSA
运行后无法显示排序后的结果,麻烦大家看看是哪里的问题,程序可以读取文件的。谢谢!
搜索更多相关主题的帖子: using public System ListNode 
2008-04-10 11:58
ywqsa206
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-4-8
得分:0 
我刚学,还不懂。。帮你顶下。。
2008-04-10 23:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-208165-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.132698 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved