求助:C#基数排序
using System;using System.Text;
using System.Collections;
using System.IO;
using System.Data;
namespace a
{
public class ListNode
{
public string[]keys;
public ListNode link;
public static int Length;
public ListNode()
{
link=null;
Length=0;
}
public ListNode(string[]key,int 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());//Console.Write("第{}条记录:",j++);
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[]rear,front;
rear=new ListNode[radix];
front=new ListNode[radix];
ListNode current=Head;
for(int i=d-1;i>=0;i--)
{
while(current!=null) //将n个对象分配到radix个队列中去
{
int k=Convert.ToInt32(current.keys[i]); //取当前检测对象的第i个关键码
if(front[k].link==null) //第k个队列空,该对象成为队头 front[k]=current;
else //不空,队尾结点链域指向它
rear[k].link=current;
rear[k]=current; //该对象成为新的队尾
current=current.link ;
}
int j=0; //依次从各个队列中把对象取出并拉链
while(front[j].link==null) j++; //找到第一个非空队列
Head=current=front[j];
ListNode last=rear[j];
for(int k=j+1;k<radix;k++) //连接其余的队列
if(front[k].link!=null)
{
last.link=front[k];
last=rear[k];
}
}
}
}
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);
}
}
}
我这个程序是基数排序,从文件中读入数据,每条记录有n个关键字.
红色部分应该有问题,麻烦各位看看
调试时错误: if(front[k].link==null) //第k个队列空,该对象成为队头
错误代码: 输入字符串的格式不正确。
--------------------编程问答-------------------- --------------------编程问答--------------------
补充:.NET技术 , C#