【问题标题】:InsertionSort on LinkedList data structure链表数据结构上的插入排序
【发布时间】: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


【解决方案1】:

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

基本上插入排序就像您对手中的卡片进行排序。您选择一个元素,从左右扫描它,直到找到适合您当前卡片的位置。想法是您手中的所有卡片都已排序,而其余的卡片则未排序。

在链表上使用相同的理论,你从头开始,拿起第一个节点,并创建一个排序列表。最后,您的输入列表应该为空,排序列表将包含所有元素的排序列表。

这是一个伪代码:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    相关资源
    最近更新 更多