【问题标题】:Crash while trying to sort a list尝试对列表进行排序时崩溃
【发布时间】:2015-08-17 11:24:24
【问题描述】:

我正在学习 C,请原谅我的任何错误。 (对我不好的英语做同样的事情,因为我不是英语。)

我必须对一个数字 int 列表进行排序。这是伪代码,它应该如何工作:

/* it takes the minimum of the entire list and put on first position, than it takes the minimum of the entire list but starting from second position, and etc... */
while(list != null){
    min = minimum(list);
    swap(min->dato, list->dato);
    list = list->next;
}

我会知道,为什么如果我创建一个排序列表,程序会崩溃? 这里,整个程序:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

struct elemento{
       int dato;
       struct elemento *next;
};

struct elemento *crealista();
void printlista(struct elemento *);
struct elemento *ordinalista(struct elemento *);
struct elemento *minimo(struct elemento *);

main(){

    struct elemento * lista = crealista();

    printf("PRIMA: \n");
    printlista(lista);
    printf("DOPO: \n");
    printlista(ordinalista(lista));

    system("PAUSE"); 

}

void printlista(struct elemento *p){

     printf("START->");
     while(p != NULL){
             printf("%d->",p->dato);
             p = p->next;
     }
     printf("NULL \n");

}

struct elemento *minimo(struct elemento *p){
       int minimo = p->dato;
       struct elemento * ritorno;
       while(p != NULL){

             if(p->dato < minimo){ minimo = p->dato; ritorno = p;}


        p= p->next;       
       }
       return(ritorno);

}
struct elemento *ordinalista(struct elemento *p){
    bool flag = false;
    int temp;
    struct elemento * start = p;
    struct elemento * min;
         while(p != NULL){ 

              min = minimo(p);
              temp = p->dato;
              p->dato = min->dato;
              min->dato = temp;

          p=p->next;
         }

    return (start);
}

struct elemento *crealista(){
    struct elemento *p,*p2; 
    int i,n;
    p = (struct elemento *)malloc(sizeof(struct elemento)); //p diventa un puntatore di tipo ELEMENTO, alla porzione di memoria restituita da malloc
    printf("Gimme first dato: "); scanf("%d",&p->dato);
    printf("\nHow many dato do u want?: "); scanf("%d",&n);
    if(n>0){
             p2 = p;
        for(i=0; i<n; i++){
              p2->next = (struct elemento *)malloc(sizeof(struct elemento));
              p2 = p2->next;
              printf("\nGimme %d dato: ",i); scanf("%d",&p2->dato);
        }
        p2->next = NULL;
    }else{p->next = NULL;}
    return (p); 
}

【问题讨论】:

  • 当然问题出在函数ordinalista....
  • p 始终不为空
  • 一点,但如果您可以edit 您的问题来(a)传播您的 cmets 以便它们在没有水平滚动的情况下可见并且(b)将您的意大利语评论翻译成英语,这将是有帮助的。
  • scanf("%d",&amp;p2-&gt;dato); --> scanf("%d",&amp;p-&gt;dato);

标签: c list sorting crash


【解决方案1】:

如果在排序列表上调用minimo(),则if(p-&gt;dato &lt; minimo)永远不会为真,因此永远不会执行{ minimo = p-&gt;dato; ritorno = p;},因此ritorno未初始化。把函数改成:

struct elemento *minimo(struct elemento *p){
       int minimo = p->dato;
       struct elemento * ritorno = p;   //this line changed!
       while(p != NULL){

             if(p->dato < minimo){ minimo = p->dato; ritorno = p;}


        p= p->next;       
       }
       return(ritorno);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多