i need help with merge sort.

struct List_A
{
  int data;
   struct List_A *next;
};
struct List_A *head;


void merge_sort(struct List_A *);


int main(void)
{
  ....lets say i have a list build --- (list_A = 4,3,2,5)
  merge_sort(List_A);
}



void merge_sort(struct List_A *head)
{
    struct List_A *cur_one;
    struct List_A *cur_two;

    if(head == NULL || head->next == NULL)
    {
        printf("emptyy list");
    }
    else
    {
     cur_one = head;
     cur_two = head->next;
     while((cur_two != NULL) && (cur_two->next != NULL))
     {
         head = head->next;
         cur_two = cur_two->next;
     }
      cur_two = head->next;
      head->next = NULL;
      return merge(merge_sort(cur_one),merge_sort(cur_two));
    }
}



struct List_A *merge (struct list_A *cur_one, struct List_A *cur_two)
{
    struct List_A *cur_three;

   if(cur_one == NULL)
   {
       return cur_two;
   }
   if(cur_two == NULL)
   {
       return cur_one;
   }
   if(cur_one->next < cur_two->age)
   {
       cur_three = cur_two;
       cur_three->next = merge(cur_one->next, cur_two);
   }
   else
   {
       cur_three = cur_two;
       cur_three->next = merge(cur_one,cur_two->next);
   }
 return cur_three;  
}

Recommended Answers

All 3 Replies

You need to tell us about the problems you are having. Would you take your car to a repair shop and tell the people "my car is broke, please fix it". Of course not, you have to explain as best you can what's wrong with your car. Same here. We can't help you if you won't let us.

what do you mean "tell us about the problem"? it just a merge sort as i say to top. if you had read my whole post you would have understand that its a linked list and each node have one data inside. the data contain 4,3,3,5. and iam trying to sort this. but for some reason its not sorting the numbers.

I tried to compile the code you posted but it contains lots of errors. What compiler are you using? If yours doesn't produce errors then you either need a different compiler or set compiler to a higher warnig level. For example, line 40 the first parameter to merge() is merge_sort() but merge_sort() has a void return value so it will not return anything.

You have to fix all those compile errors before you can tell whether or not the code will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.