Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用双链接列表对值进行排序_C#_Algorithm_Sorting_Dll_Doubly Linked List - Fatal编程技术网

C# 使用双链接列表对值进行排序

C# 使用双链接列表对值进行排序,c#,algorithm,sorting,dll,doubly-linked-list,C#,Algorithm,Sorting,Dll,Doubly Linked List,我正在用一些简单的按钮制作一个WFA,用随机数填充数组/列表,然后尝试对它们进行排序。我只是不能正确地分类。谢谢你的帮助 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; name

我正在用一些简单的按钮制作一个WFA,用随机数填充数组/列表,然后尝试对它们进行排序。我只是不能正确地分类。谢谢你的帮助

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoublyLinkedList1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Sortbtn_Click(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            int[] array = new int[listBox1.Items.Count];
            for (int i = 0; i < array.Length; i++)
                {
                    array[i] = int.Parse(listBox1.Items[i].ToString());
                }
            array = cN.notearray(array);
            for (int i = 0; i < array.Length; i++)
            {
                listBox2.Items.Add(array[i]);
            }
        }

        private void filllstbtn_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            Random rd = new Random();
            for (int i = 0; i < 8; i++)
            {
                listBox1.Items.Add(rd.Next(1, 9));  
            }

            }
        }

    }
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间双链接列表1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效排序单击(对象发送方,事件参数e)
{
listBox2.Items.Clear();
int[]数组=新的int[listBox1.Items.Count];
for(int i=0;i
cN类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DoublyLinkedList1
{
    class cN
    {
        public int V;
        public cN L;
        public cN R;
        private static cN[] n_array;
        private static cN head = null;

        public cN(cN nR, cN nL, int nV)
        {
            V = nV;
            L = nL;
            R = nR;
        }

        public static int[] notearray(int[] array)
        {
            n_array = new cN[array.Length];
            //Fill array with notes
            for (int i = 0; i < array.Length; i++)
            {
                n_array[i] = new cN(null, null, array[i]);
            }
            head = n_array[0];
            //sort array
            for (int i = 0; i < n_array.Length; i++)
            {
                sortnode(head, n_array[i]);
            }
            return array_composed();

        }

        private static void sortnode(cN chead, cN node)
        {
            if (node.V > chead.V)
            {
                if (chead.R != null)
                {
                    sortnode(chead.R, node);
                }
                 else 
                {
                    chead.R = node;
                    node.L = chead;
                }
            }
            else
            {
                if (head == chead)
                {
                    head = node;
                    node.R = chead;
                    chead.L = node;
                }
                else
                {
                    node.R = chead;
                    node.L = chead.L;
                    chead.R = node;
                    chead.L = node;
                }
            }



        }

        private static int[] array_composed()
        {
            int[] iarr = new int[n_array.Length];
            cN chead = head;
            int counter = 0;
            while (chead != null)
            {
                iarr[counter] = chead.V;
                chead = chead.R;
                counter++;
            }
            iarr[counter] = chead.V;
            return iarr;
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间双链接列表1
{
类别cN
{
公共INTV;
公共图书馆;
公共cN R;
私有静态cN[]n_数组;
私有静态cN头=空;
公共cN(cN编号、cN nL、int nV)
{
V=nV;
L=nL;
R=nR;
}
公共静态int[]notearray(int[]数组)
{
n_array=新的cN[array.Length];
//用注释填充数组
for(int i=0;ichead.V)
{
if(chead.R!=null)
{
sortnode(chead.R,node);
}
其他的
{
chead.R=节点;
node.L=chead;
}
}
其他的
{
如果(head==chead)
{
头部=节点;
node.R=chead;
chead.L=节点;
}
其他的
{
node.R=chead;
node.L=chead.L;
chead.R=节点;
chead.L=节点;
}
}
}
私有静态int[]数组_组合()
{
int[]iarr=newint[n_array.Length];
cN chead=头部;
int计数器=0;
while(chead!=null)
{
iarr[计数器]=chead.V;
chead=chead.R;
计数器++;
}
iarr[计数器]=chead.V;
返回iarr;
}
}
}

我的sortnode方法似乎有问题。它在一个无限循环中不断重复出现,我似乎无法让它正常工作。

为了简化问题,如果您想将一个值数组转换为一个排序的双链接列表,我希望这部分正确。老实说,我建议你改变你的做法

我建议首先实现一些重要的方法,比如在列表中插入一个新的cN。 下面是如何编写notearray函数:

public static int[] notearray(int[] array)
{
      n_array = new cN[array.Length];
      for(int i=0 ; i<array.Length ; i++)
      {
          n_array[i] = insertItem(array[i]);
      }
}

private static cN insertItem(int value)
{
      cN item = new cN(null,null,value)
      int i=0;
      while(n_array[i]!=null && n_array[i].V < value)
      {
          i++;
      }

      /*
        remember to check special conditions like if this new item is going to be head or going
        to be last item in the list or both in case of empty list
      */


      item .L = n_array[i].L;
      item .R = n_array[i];
      n_array[i].L = item;

      return item;
}
cn iterator = head;
for(...)
{
    doSomthing(iterator);
    iterator = iterator.R;
}