【发布时间】:2017-03-01 16:30:11
【问题描述】:
我有一个包含此代码的 List.cs 文件:
class MyDataList : DataList
{
class MyLinkedListNode
{
public MyLinkedListNode nextNode { get; set; }
public double data { get; set; }
public MyLinkedListNode(double data)
{
this.data = data;
}
}
MyLinkedListNode headNode;
MyLinkedListNode prevNode;
MyLinkedListNode currentNode;
public MyDataList(int n, int seed)
{
length = n;
Random rand = new Random(seed);
headNode = new MyLinkedListNode(rand.NextDouble());
currentNode = headNode;
for (int i = 1; i < length; i++)
{
prevNode = currentNode;
currentNode.nextNode = new MyLinkedListNode(rand.NextDouble());
currentNode = currentNode.nextNode;
}
currentNode.nextNode = null;
}
public override double Head()
{
currentNode = headNode;
prevNode = null;
return currentNode.data;
}
public override double Next()
{
prevNode = currentNode;
currentNode = currentNode.nextNode;
return currentNode.data;
}
public override void Swap(double a, double b)
{
prevNode.data = a;
currentNode.data = b;
}
}
还有一个带有此代码的 Program.cs
class Insertion_Sort
{
static void Main(string[] args)
{
int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;
Test_Array_List(seed);
}
public static void InsertionSort(DataList items)
{
}
public static void Test_Array_List(int seed)
{
int n = 12;
MyDataList mylist = new MyDataList(n, seed);
Console.WriteLine("\n LIST \n");
mylist.Print(n);
InsertionSort(mylist);
mylist.Print(n);
}
}
abstract class DataList
{
protected int length;
public int Length { get { return length; } }
public abstract double Head();
public abstract double Next();
public abstract void Swap(double a, double b);
public void Print(int n)
{
Console.Write(" {0:F5} ", Head());
for (int i = 1; i < n; i++)
Console.Write(" {0:F5} ", Next());
Console.WriteLine();
}
}
有人可以在这里帮助我进行插入排序吗?尽管网上有很多示例,但我无法让代码正常工作。
【问题讨论】:
-
到目前为止,您是如何尝试实现 InsertionSort 的?尝试过的方法没有奏效?
-
我已尝试重新创建此 codereview.stackexchange.com/questions/59968/…,但我无法重新创建没有索引且仅使用“nexts”的所有内容
标签: java c# list linked-list insertion-sort