473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sorting a singly linked list

Hello,
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;
  6.  
  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.     }
  33.  
Mar 8 '08 #1
3 3379
Banfa
9,065 Expert Mod 8TB
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
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
Banfa
9,065 Expert Mod 8TB
I think that the logic is wrong because in the case of

a->messageId > b->messageId is TRUE
AND
a == first is TRUE
AND
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 http://en.wikipedia.org/wiki/Sorting_algorithms.

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

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

Similar topics

4
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
4
by: Peter Schmitz | last post by:
Hi, in my application, I defined a linked list just as follows: typedef struct _MYLIST{ int myval; void *next; }MYLIST; MYLIST head;
59
by: Anando | last post by:
Hi, I have a linear singly linked list of 100 elements. I would like to prune it such that only user specified elements (say only the elements 1, 13, 78 and 100 of the original list) survive...
3
by: malik | last post by:
// Linked Lists in classes(excluding structures) without using tail pointer # include<iostream.h> # include<stdlib.h> void Swap(int num1, int num2) { int a = num1; num2 = num1; num2 = a;
6
by: Alien | last post by:
Hi, I am having problem with ordering a singly linked list. Since a singly linked list goes only one way, is it possible to order them? If my structs look like the one below. struct block...
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
7
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.