奇偶数随机指针,链表

时间:2016-11-26 15:22:16

标签: c list pointers nullreferenceexception

我需要以这样的方式修改SLL随机指针:列表中的所有奇数都是通过随机指针连接的,它们的顺序与SLL相同,偶数相同。我的意思是... 我需要修改随机指针,使得随机列表中的First Odd数字随机 指向列表中的第二个奇数,依此类推。 我返回一个数组[odd_count,even_count]。 这是我的代码...我得到NullReferenceException。我无法找到我的代码中指针处理不当的地方......这是我的代码......

#include <stdlib.h>
#include <stdio.h>

struct oddevennode{
    int data;
    struct oddevennode * next;
    struct oddevennode * random;
};

int * oddeven_sll(struct oddevennode *head){
    if (head == NULL) return NULL;
    int final_array[2]; 
    int odd_count = 0; int even_count = 0; 
    oddevennode *front = head; oddevennode *front_odd = head; oddevennode *front_even = head; 
oddevennode *odd_pt_front = front_odd; oddevennode *odd_pt_rear = front_odd;
oddevennode *even_pt_front = front_even; oddevennode *even_pt_rear = front_even;
    while (front != NULL){
        if (front->data % 2 == 0) even_count++;
        else odd_count++;
        front = front->next;
    }
    final_array[0] = odd_count;
    final_array[1] = even_count;
    if (odd_count != 0 && even_count != 0){
       while (front_odd->next != NULL){
           if (front_odd->data % 2 == 0){
               front_odd = front_odd->next;
               odd_pt_front = odd_pt_front->next;
               odd_pt_rear = odd_pt_rear->next;
           }
           else if (front_odd->data % 2 == 1){
               odd_pt_rear = odd_pt_rear->next;
               while (odd_pt_rear->data % 2 == 0){
                   odd_pt_rear = odd_pt_rear->next;
                   if (odd_pt_rear->next == NULL) break;
               }
               odd_pt_front->random = odd_pt_rear;
               odd_pt_front = odd_pt_rear;
               front_odd = odd_pt_rear;
           }
           if (odd_pt_rear->next == NULL) break;
        }
        while (front_even->next != NULL){
           if (front_even->data % 2 == 1){
               front_even = front_even->next;
               even_pt_front = even_pt_front->next;
               even_pt_rear = even_pt_rear->next;
           }
           else if (front_even->data % 2 == 0){
               even_pt_rear = even_pt_rear->next;
               while (even_pt_rear->data % 2 == 1){
                   even_pt_rear = even_pt_rear->next;
                   if (even_pt_rear->next == NULL) break;
               }
               even_pt_front->random = even_pt_rear;
               even_pt_front = even_pt_rear;
               front_even = even_pt_rear;
           }
           if (even_pt_rear->next == NULL) break;
       }
    }
    return final_array;
}

请帮我在上述实施中找到我的错误... 例如:10-> 3-> 6-> 5-> 8-> 1-> 3

3-> 5-> 1-> 3 [奇数从3穿过Randoms] 10-> 6-> 8 [偶数通过Randoms从10开始] 提前谢谢

0 个答案:

没有答案