0

I need to modify the SLL random pointers in such a way that ,All odd numbers in the list are connected by random pointers in the same order in which they are in SLL and the same for even numbers. I mean... I need to modify random pointers in such a way that First Odd number in List random will point to second odd number in the list and so on. I return a array [odd_count,even_count]. here is my code...I am getting NullReferenceException. I am not able find out where the pointer is not handled properly in my code...here is my code...

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

please help me to find my mistake in the above implementation... eg: 10->3->6->5->8->1->3

3->5->1->3 [Odd numbers if traversed through Randoms from 3] 10->6->8 [Even numbers if traversed through Randoms from 10] thanks in advance

4
  • Please include the code which calls this. Thanks. Commented Nov 26, 2016 at 15:37
  • NullReferenceException suggests that you are using Visual Studio. I don't have experience debugging in Visual Studio, but this answer suggests that you can configure Visual Studio so that it breaks on that error in such a way that you can see what line is causing it: stackoverflow.com/a/4719165/4996248 Commented Nov 26, 2016 at 15:40
  • it helped alot. thank you @JohnColeman Commented Nov 26, 2016 at 17:15
  • two pointers to pointer to oddeven node could simplify your code. a lot. Commented Nov 26, 2016 at 18:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.