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
- if(first != NULL && first->next != NULL) /* less than two nodes */
- {
- message *a = first;
- message *b = first->next;
- message *beforeA = NULL;
- while(b != NULL)
- {
- if(a->messageId > b->messageId)
- {
- a->next = b->next;
- b->next = a;
- if(a == first && beforeA == NULL)
- {
- first = b;
- beforeA = first;
- }
- else if (beforeA != NULL)
- {
- beforeA->next = b;
- }
- /* Move to the next couple of nodes */
- b = a->next;
- beforeA = beforeA->next;
- }else if (a->messageId < b->messageId)
- {
- beforeA = a;
- a = a->next;
- b = b->next;
- }
- }
- }