2016-10-01 60 views
1

我有一个链表,我必须确保无论列表的最大数量是多少,我都会将它放在最后链表。我不确定我做错了什么。有人可以解释吗?链接链接函数不会将最大项目放在列表末尾,如我所愿

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; 
     } 
    } 

} 

回答

0

这应该工作,虽然我没有测试过它。如果是家庭作业,请今天不要阅读答案(向自己学习会帮助你更多!)。

做一些你的列表元素的绘制,看看链接的行为。你会立即看到这个方法的问题。

我的图示例

list drawing

校正(不立即读取)

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++; //not used, may be cleaned up 
     } 
     //else{ //else is not needed the code would be executed in next loop anyway 
     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=curr1; // not prev->next; 
      // prev->next=curr1->next; cur1 has not changed you re-do exactly the same 
      // curr1->next=tmp1; 
      curr1->next=NULL; //it will be the last element 
     } 
     else{ 
      prev=prev->next; 
      curr1=curr1->next; 
     } 
    } 
    curr1->next = tmp1; //add the element we cut before 
} 
+0

是有办法,我只能让它动它的一个节点,直到它到底还是我必须这样做? – TheOne817

+0

@vic有几百种方法可以完成所有的工作;)可以尝试将你的2个循环和逐个节点一个一个地排序,完全排序列表将是一个好主意。做图纸,如果它不足以使它工作,做更多的图纸。 – Julien

0

问题(1)

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; 

排除第一个元素。 (2)

prev->next=curr1->next;//curr1 cut off 
tmp1=prev->next; 
prev->next=curr1->next;//Processing is a duplicate. meaningless. 
curr1->next=tmp1;//==> curr1->next= prev->next ==> curr1->next= curr1->next;meaningless. 

具体过程不存在(如果第一元素是一个虚设没问题。)

问题。

修复这样的: (注:第一个因素是处理,如果这不是假的变化是很容易)

void moveAllMaxAtEnd(list A){ 
    if(!A || !A->first || !A->first->next) 
     return; 

    link prev = A->first; 
    link curr = prev->next; 
    int max = prev->item; 

    while(curr != NULL){//This first while is where I find the max item 
     if(curr->item >= max){ 
      max = curr->item; 
     } 
     prev = curr; 
     curr = curr->next; 
    } 
    link last = prev; 

    prev = NULL; 
    curr = A->first; 
    link max_list = NULL; 

    while(curr->next != NULL){ 
     if(curr->item == max){ 
      link cut = curr; 
      curr = curr->next; 
      if(prev){ 
       prev->next = cut->next; 
      } else { 
       A->first = cut->next; 
      } 
      cut->next = max_list; 
      max_list = cut; 
     } else { 
      prev = curr; 
      curr = curr->next; 
     } 
    } 
    last->next = max_list; 
}