sorting in link list
Mar 29, 2016 at 4:37pm UTC
not getting sorting output need help tomorrow is submission
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
void sorting()
{
int tid=0,count=0;
struct node *q;
q=start;
while (q!=NULL)
{
q=q->next;
count++;
}
q=start;
for (int j=0;j<count;j++)
{
while (q->next=NULL)
{
if (q->id > q->next->id)
{
tid=q->id;
q->id=q->next->id;
q->next->id=tid;
break ;
}
q=q->next;
}
// start=q;
}
Mar 29, 2016 at 4:40pm UTC
We don't know what the struct node has. Only 'next' and 'id'?
Why do you assign a nullptr
to a pointer within a condition expression on line 16?
Mar 29, 2016 at 4:43pm UTC
You're really not providing enough information. But as a guess since you're title states you're working with a linked list shouldn't you be swapping the "structures" not the individual struct members?
Mar 29, 2016 at 8:14pm UTC
Consider using a for loop for iterating through the list:
1 2 3
for (q=start; q != NULL; q = q->next) {
do_stuff;
}
As for the sort algorithm, move line 13 to jus be fore line 16.
Mar 29, 2016 at 8:15pm UTC
Consider using a for loop for iterating the list:
1 2 3
for (q = start; q !=NULL; q = q->next) {
do_stuff;
}
If you do this, you'll probably realize that line 13 is out of place. It belongs with line 16.
Topic archived. No new replies allowed.