By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

sorting a singly linked list

P: 3
I am trying to sort a singly linked list for the following typedef
typedef struct message {
int messageId;
char * messageText;
struct message * next;
} message;
I have successfully implemented add, delete, print, and search functions for the linked list but I am not sure why I am stuck with this one.

I posted my code below. I get a BUS error in the second iteration of the while loop. I am quite new to linked list so I will appreciate any explanation.

Thank you

Expand|Select|Wrap|Line Numbers
  1.     if(first != NULL && first->next != NULL) /* less than two nodes */
  2.     {        
  3.         message *a = first;
  4.         message *b = first->next;
  5.         message *beforeA =  NULL;
  7.         while(b != NULL)
  8.         {
  9.             if(a->messageId > b->messageId)
  10.             {
  11.                 a->next = b->next;
  12.                 b->next = a;
  13.                 if(a == first && beforeA == NULL)
  14.                 {
  15.                     first = b;
  16.                     beforeA = first;
  17.                 }
  18.                 else if (beforeA != NULL)
  19.                 {
  20.                     beforeA->next = b;
  21.                 }
  22.                 /* Move to the next couple of nodes */
  23.                 b = a->next;
  24.                 beforeA = beforeA->next;
  25.             }else if (a->messageId < b->messageId)
  26.             {
  27.                                 beforeA = a;
  28.                 a = a->next;
  29.                 b = b->next;
  30.             }
  31.         }
  32.     }
Mar 8 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,916
Although that is not how I might write the code I am finding it hard to see any error in it. Is it possible that the data in the list has been de-allocated somehow before sorting. Otherwise I think we are going to need to see the input data to the function in order to be able to diagnose the problem.
Mar 9 '08 #2

P: 3
I don't think this has to do with the input. I guess there is something wrong with the implementation. But here is a sample input.
4 sample text
1 another sample text
8 one more sample
6 more sample

How would you write the code?

Thank you for your reply
Mar 9 '08 #3

Expert Mod 5K+
P: 8,916
I think that the logic is wrong because in the case of

a->messageId > b->messageId is TRUE
a == first is TRUE
beforeA == NULL is TRUE

as would be the case in the data you have provided at the end of the first iteration beforeA == A I think (checked on paper), you could verify this using a debugger.

I assume beforeA is actually meant to point to the entry before a and not the same entry as a so the logic has gone wrong.

If I wrote this I would not have variables a and b as you have them, that is I would only have 1 variable, a, maintained by the loop and I would create b from a as required. 1 less thing to keep track of.

And finally even if you fix the logic errors this sorting algorithm will not work take this data example

9 Some text
10 More text
7 Further text

Your algorithm makes a single pass checking entry N against N+1 so after sorting the order would be

9 Some text
7 Further text
10 More text

which is still wrong.

You need to implement a proper sorting algorithm and rather than trying to make one up yourself I suggest looking one up

Another alternitive is to create the list sorted (I often do this) that is don't just create the linked list any old way but when you add an entry add it in the correct place to preserve your sorting (and that is a way of sorting too take entries off the current list and insert them into a new list in sorted order).
Mar 9 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.