2016-07-06 4 views
1

の奇数ソートも以前の作業コードが誰かによって変更され、現在はエラーを出しています。 私はプッシュすると、常にヌルノードを持っています。リンクリストのc#

pushメソッドでは、常にnullを渡します。

using System; 

/* a node of the singly linked list */ 
public class node 
{ 
    public int data; 
    public node next; 
} 

public static class GlobalMembers 
{ 
    public static void segregateEvenOdd(node head_ref) 
    { 
     node end = head_ref; 
     node prev = null; 
     node curr = head_ref; 

     /* Get pointer to the last node */ 
     while (end.next != null) 
     { 
      end = end.next; 
     } 

     node new_end = end; 

     /* Consider all odd nodes before the first even node 
      and move then after end */ 
     while (curr.data % 2 != 0 && curr != end) 
     { 
      new_end.next = curr; 
      curr = curr.next; 
      new_end.next.next = null; 
      new_end = new_end.next; 
     } 

     // 10->8->17->17->15 
     /* Do following steps only if there is any even node */ 
     if (curr.data % 2 == 0) 
     { 
      /* Change the head pointer to point to first even node */ 
      head_ref = curr; 

      /* now current points to the first even node */ 
      while (curr != end) 
      { 
       if ((curr.data) % 2 == 0) 
       { 
        prev = curr; 
        curr = curr.next; 
       } 
       else 
       { 
        /* break the link between prev and current */ 
        prev.next = curr.next; 

        /* Make next of curr as NULL */ 
        curr.next = null; 

        /* Move curr to end */ 
        new_end.next = curr; 

        /* make curr as new end of list */ 
        new_end = curr; 

        /* Update current pointer to next of the moved node */ 
        curr = prev.next; 
       } 
      } 
     } 

     /* We must have prev set before executing lines following this 
      statement */ 
     else 
     { 
      prev = curr; 
     } 

     /* If there are more than 1 odd nodes and end of original list is 
      odd then move this node to end to maintain same order of odd 
      numbers in modified list */ 
     if (new_end != end && (end.data) % 2 != 0) 
     { 
      prev.next = end.next; 
      end.next = null; 
      new_end.next = end; 
     } 
     return; 
    } 


    public static void push(node head_ref, int new_data) 
    { 

     node new_node =new node();// = (node) malloc(sizeof(node)); 


     new_node.data = new_data; 

     /* link the old list off the new node */ 
     new_node.next = head_ref; 

     /* move the head to point to the new node */ 
     head_ref = new_node; 
    } 

    /* Function to print nodes in a given linked list */ 
    public static void printList(node node) 
    { 
     while (node != null) 
     { 
      Console.Write("{0:D} ", node.data); 
      node = node.next; 
     } 
    } 

    /* Drier program to test above functions*/ 
    static int Main() 
    { 

     node head = null; 

     /* Let us create a sample linked list as following 
      0->2->4->6->8->10->11 */ 

     push(head, 11); 
     push(head, 10); 
     push(head, 8); 
     push(head, 6); 
     push(head, 4); 
     push(head, 2); 
     push(head, 0); 

     Console.Write("\nOriginal Linked list \n"); 
     printList(head); 

     segregateEvenOdd(head); 

     Console.Write("\nModified Linked list \n"); 
     printList(head); 

     return 0; 
    } 
} 
+2

あなたは 'ノードヘッド=ヌルを初期化している;'メイン() 'の最初の行に'た後、繰り返しとしてnullを追加しよう'Push()'メソッドを使って参照ノードに渡します。値で 'head'を初期化してみてください。 –

+0

先生、 'node new_node = head_ref''を意味しますか – vish1990

答えて

3

変更するには、このようなリスト:

public static node push(node head_ref, int new_data) { 


    node new_node = new node(); // = (node) malloc(sizeof(node)); 

    new_node.data = new_data; 

    /* link the old list off the new node */ 
    new_node.next = head_ref; 

    /* move the head to point to the new node */ 
    head_ref = new_node; 
    return head_ref; 
} 

static int Main() 
{ 

    node head = null; 

    /* Let us create a sample linked list as following 
     0->2->4->6->8->10->11 */ 


     head = push(head, 11); 
+0

ありがとう:) – vish1990

関連する問題