LinkedListSort在LinkedList数据结构上

时间:2017-03-01 16:30:12

标签: java c# list linked-list insertion-sort

我有一个带有以下代码的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吗?即使网上有很多例子,我也无法让代码工作。我无法让它在我的案例中运作。

1 个答案:

答案 0 :(得分:0)

我将尝试回答这个问题,为您提供有关如何在链表上使用插入排序的tid-bits。希望您能够在代码中学习并应用它。

基本上,插入排序就像你手中的卡片。您从左右选择一个元素扫描,直到找到当前卡适合的位置。想法是你手中的所有牌都被分类,而剩下的牌是未分类的。

在链接列表中使用相同的理论,从头开始,选择第一个节点,然后创建一个排序列表。最后,您的输入列表应为null,排序列表将列出所有元素的列表。

这是一个伪代码:

struct node* sort_link_list(struct node* plist) {

    /* empty list or only one element in a list */

    if(plist == null or plist->next == null) {
        return plist;
    }
    /* Head of sorted list, initially sorted list in null */

    struct node * head = null; 

    /* Scan each element of the input list */
    while(plist != null) {
        /* temp is the current node to be placed in sorted list */
        struct node* temp  = plist;
        /* input list is one short */
        plist = plist->next;

       /* if sorted list is empty or current node is to be placed in front */

        if(head == null or temp->val < head->val ) {
            temp->next = head;
            head = temp;
        }else {
            /* scan the sorted list and place temp in the appropriate location */
            struct node *p = head;
            while(p != null) {
                /* position found or you reached end of the sorted list*/ 
                if(p->next == null or temp->val < p->val) {
                    temp->next = p->next;
                    p->next = temp;
                    break;    
                }
                p = p->next;
            }    
        }                
    }
    return head;
}