What is wrong with my Linked List sorting method? I would appreciate some help
Nov 12, 2013 at 4:14am UTC
I don't know why it doesn't sort it. It just spits out the numbers in the same way that I entered them. I have looked and looked but I don't think my mind is in the right place today. What am I missing?
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
int main()
{
ListNode *head=NULL;
ListNode *current=NULL;
ListNode *tail=NULL;
ListNode *min=NULL;
double nums;
for (int i=0;i<5;i++)
{
cout<<"Enter numbers: " ;
cin>>nums;
if (i==0)
{
head=new ListNode(nums,NULL);
tail=head;
}
else
{
current=new ListNode(nums,NULL);
tail->next=current;
tail=current;
}
}
system("CLS" );
bool swapped=true ;
current=head;
while (swapped)
{
while (current->next!=NULL)
{
min=head;
if (current->number<current->next->number)
{
swapped=true ;
if (head==current)
{
head=current->next;
current->next=head->next;
head->next=current;
min=head;
}
else
{
min->next=current->next;
current->next=min->next->next;
min->next->next=current;
}
current=current->next;
}
else if (head!=current)
{
min=min->next;
}
}
}
tail=head;
while (tail!=NULL)
{
cout<<tail->number<<endl;
tail=tail->next;
}
return 0;
}
Last edited on Nov 12, 2013 at 5:00am UTC
Nov 12, 2013 at 5:00am UTC
Bumping, could really use some help on this.
Nov 12, 2013 at 9:57am UTC
It just spits out the numbers in the same way that I entered them.
It's not possible.
swapped
is never set to
false
hence on line 34 you have an infinite loop and it cannot show the result
Topic archived. No new replies allowed.