Home > database >  Function stuck while splitting a list of nodes in two lists, but only when the splitted lists contai
Function stuck while splitting a list of nodes in two lists, but only when the splitted lists contai

Time:01-08

The function split takes as parameter one list variable, which is a just a pointer to a node element, with its int val and node * next variables; that function should return an array of two lists, one for even numbers, and one for odd numbers. I'm just returning a void at the moment, just because my first goal is to make the split function to do what it's supposed to do, then I'll just return the result (was thinking of returning an array of structs).

The trouble is, the function split looks stuck when my_list contains at least two even or two odd numbers (which means will always be stuck when it does contains at least three nodes).

As soon as I'm inserting a maximum of two nodes (one even, and one odd numbers), the function works as expected, while when my_list contains at least two even numbers or two odd numbers, the console just looks stuck: it doesn't even print the first printf command of the function, and every digit I put on the stdin just do nothing anymore; I don't receive any segmentation fault error.

The issue does not show up when I comment the two lines list_even = insert(list_even, n1) and list_odd = insert(list_odd, n1): why is that, how to solve it, and why it doesn't happen when my_list contains one even number and one odd number? I'm using exactly in the same way the insert method that I've used in the list.c file.

It looks like list_even and list_odd cannot contains more than one element each, or the function get stuck, but why?

LIST.C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list.h"
void help();

int main(){
   list my_list = L_EMPTYLIST;
   help();
   
   ...
   
   while(chose!=0){
      switch(chose){
         case 1:
            puts("Insert an int.");
            getchar();
            int item = 0;
            scanf("%d", &item);
            
            node *my_node = malloc(sizeof(struct _node));
            my_node->val = item;
            my_node->next = NULL;
            
            my_list = insert(my_list, my_node);
            break;
         case 2:
            print(my_list);
            break;
         case 3:
            split(my_list);
            break;
         case 4:
            my_list = clear(my_list);
            break;
      }
      
      ...

   }
   
}

void help(){
   puts("1: Insert node.");
   puts("2: Print the list.");
   puts("3: Split the list.");
   puts("4: Clear the list.");
   puts("0: Exit.");
}

LIST.H

// Definitions of the structures
typedef struct _node node;
typedef node *list;

struct _node{
   int val;
   struct _node *next;
};

struct _node nodes[2]; //Not used yet

#define L_EMPTYLIST NULL

// Operations on the list above
list insert(list my_list, node* my_node){
   if(my_list==L_EMPTYLIST){
      //List is empty
      my_list = my_node;
   } else {
      node *head = my_list;
      while(head->next!=NULL){
         head = head->next;
      }
      head->next = my_node;
   }
   return my_list;
}

void split(list my_list){
   printf("hello"); // THIS NOT EVEN PRINTED WHEN my_list contains two even numbers or two odd numbers (?)
   list list_even = L_EMPTYLIST;
   list list_odd = L_EMPTYLIST;
   
   if(my_list==L_EMPTYLIST){
      puts("List is empty: cannot split.");
   } else {
     node *n1 = my_list;
     while(n1!=NULL){
        if((n1->val)%2==0){
           //n pari
           list_even = insert(list_even, n1); //This cause the issue
        } else {
           list_odd = insert(list_odd, n1); //This cause the issue
        }
        n1=n1->next;
     }
     print(list_even);
     print(list_odd);
   }
}

void print(list my_list){...}
list clear(list my_list){...}

CodePudding user response:

So it looks the problem was adding directly the node n1 to list_even and list_odd, just because node *n1 = my_list, which means I'm not only adding the single even or the odd node, but also its next values: in practice I'm appending the whole list in list_even and list_odd, starting respectively from its first even and odd nodes.

Then,in the insert function I'm reaching the end of list_even and list_odd, but I'm replacing its NULL value in the next with the second even or odd value: once the NULL value is overwritten, I can't get out from the insert function.

I basically obtain something like this:

list: (5)->(10)->(15)->(20)->(25)->(30)->NULL

list_even (first step): (10)->(15)->(20)->(25)->(30)->NULL

list_even (second step): (10)->(15)->(20)->(25)->(30)->(20)->(25)->(30)

Fixed split function:

void split(list my_list){
   printf("ciao");
   list list_even = L_EMPTYLIST;
   list list_odd = L_EMPTYLIST;
   
   if(my_list==L_EMPTYLIST){
      puts("List is empty: cannot split.");
   } else {
     node *n1 = my_list;
     while(n1!=NULL){
        node *new_node = malloc(sizeof(struct _node));
        new_node->val = n1->val;
        new_node->next = NULL; // THE PROBLEM WAS FORGETTING THIS
        if((n1->val)%2==0){
           //n pari
           list_even = insert(list_even, new_node);
        } else {
           list_odd = insert(list_odd, new_node);
        }
        n1=n1->next;
     }
     print(list_even);
     print(list_odd);
   }
}
  • Related