【问题标题】:Linked link function won't put the max item at the end of the list, as intended链接链接功能不会按预期将最大项目放在列表的末尾
【发布时间】:2016-10-01 18:18:09
【问题描述】:

我有一个链表,我必须确保无论链表的最大数量是多少,我都将它放在链表的末尾。我不确定我做错了什么。谁能解释一下?

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

}

【问题讨论】:

    标签: c linked-list


    【解决方案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++; //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
    }
    

    【讨论】:

    • 有没有办法让它移动一个节点直到它结束,还是我必须这样做?
    • @vic 有数百种方法可以做任何事情;) 可以尝试对您的 2 while 循环进行分组并逐个移动节点以对列表进行完全排序是个好主意。画图,如果还不够,就画更多图。
    【解决方案2】:

    问题(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.
    

    具体进程不存在。

    像这样修复: (注:如果不是dummy,则第一个元素正在处理。更改很容易)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多