在“节点”列表中,所有EVEN值都位于开头,结尾处为几率

时间:2018-11-27 13:48:22

标签: c# linked-list

那是高中问题类型,因此不允许使用“重武器”,也不允许与数组一起使用,问题是:假设我有节点列表,我应该将所有带有偶数值的节点放在列表和所有带有奇数值的节点,我应该放在列表的末尾,即:

3,2,7,6,9,4 ---> 2,6,4,3,7,9

节点类:

public class Node
{
    public int value;
    public Node next;

    public Node(int value)
    {
        this.value = value;
        this.next = null;
    }

    public Node(int value, Node next)
    {
        this.value = value;
        this.next = next;
    }

    public void SetNext( Node next)
    {           
        this.next = next;
    }

    public void SetValue(int value)
    {
        this.value = value;
    }

    public Node GetNext()
    {
        return this.next;
    }

    public int GetValue()
    {
       return this.value;
    }

}

主类:

    Node n1 = new Node(3);
    Node n2 = new Node(2);
    Node n3 = new Node(1);
    Node n4 = new Node(4);
    Node n5 = new Node(9);
    Node n6 = new Node(6);

    n1.SetNext(n2);
    n2.SetNext(n3);
    n3.SetNext(n4);
    n4.SetNext(n5);
    n5.SetNext(n6);
    Ex(n1); // Invoking the function

我已经开始了类似的事情:

 private void Ex(Node list)
        {
            Node temp = list;        
            while (temp != null)
            {
                if (temp.GetValue() % 2 == 0)
                {

                }
            }          
        }

0 个答案:

没有答案