void Linkedlist::sort(int num)
{
node *q;
node *a;
int input=num;
a = new node;
a->data=input;
a->link=NULL;
q=head;
while(q->link !=NULL)
{
int temp=0;
temp = q->link->data - input;
if(temp>0)
{
a->link=q->link;
q->link=a;
break;
}
}
q=q->link;
if (q->link = NULL)
{
q->link = a;
}
}
I get a crash and I've narrowed it down to the last if statement. Also this function is meant to handle new members after 1 member has been added. Where am I going wrong?
What sorting algorithm are you trying to implement?
Line 29:
- You assign q->link to NULL then line 32 you try to dereference a null pointer
- Either line 27 or 29 could crash the program as soon as line 32 is executed - look at the condition of your while loop
I was trying to make it sort so that if node *a was less than the element node *q was pointing to, it would point a->link=q->link and q->link=a and swap the a and q's positions.