我有一个链表,我必须确保无论列表的最大数量是多少,我都会将它放在最后链表。我不确定我做错了什么。有人可以解释吗?链接链接函数不会将最大项目放在列表末尾,如我所愿
void moveAllMaxAtEnd(list A) {
int max=0;
link tmp=A->first;
link curr=tmp->next;
int i,count=0;
while(curr!=NULL){ //This first while is where I find the max item
if(curr->item>=max){
max=curr->item;
count++;
}
else{
curr=curr->next;
}
}
link prev=A->first;
link curr1=prev->next;
link tmp1;
while(curr1->next!=NULL){ //In this loop I am trying to put the
if(curr1->item==max){ //max items at the end.
prev->next=curr1->next;
tmp1=prev->next;
prev->next=curr1->next;
curr1->next=tmp1;
}
else{
prev=prev->next;
curr1=curr1->next;
}
}
}
是有办法,我只能让它动它的一个节点,直到它到底还是我必须这样做? – TheOne817
@vic有几百种方法可以完成所有的工作;)可以尝试将你的2个循环和逐个节点一个一个地排序,完全排序列表将是一个好主意。做图纸,如果它不足以使它工作,做更多的图纸。 – Julien