while循环终止后的代码无法运行

时间:2019-05-18 10:05:00

标签: c sorting while-loop

我正在编写一种对链表进行排序的算法。但是,我遇到了一个问题,即我的while循环似乎已终止,但以下代码均无法运行。我的while循环终止可能是非常错误的,但是根据我的打印输出,已经满足了while循环的条件(cur!= NULL)。

void LL_sort (struct node **headpp){
//if list is null or size 1
if((*headpp)==NULL || (*headpp)->next ==NULL){
    return;
}
struct node* sortedHead = NULL;
struct node* sortedCur = NULL;
struct node* cur = *headpp;
struct node* fwd = (*headpp)->next;
int emptyflag = 0;

while(cur!=NULL){
    fwd=cur->next;
    printf("start of loop cur is %p wtih value %i\n", cur, cur->data);
    printf("start of loop fwd is %p\n", fwd);
    //if sorted list is empty
    if(sortedHead == NULL){
        cur->next = NULL;
        sortedHead = cur;
        emptyflag = 1;
        printf("[sorted list NULL]sorted head is %p with value %i\n", sortedHead, sortedHead->data );
        printf("[sorted list NULL]reg head is %p with value %i\n", *headpp, (*headpp)->data );
    }
    //if cur is smaller than head
    if(sortedHead->data >= cur->data && emptyflag==0){
        cur->next = sortedHead;
        sortedHead = cur;
        printf("[sortedHead change]sorted head is %p with value %i\n", sortedHead, sortedHead->data );
        printf("[sortedHead change]reg head is %p with value %i\n", *headpp, (*headpp)->data );
    }

    sortedCur=sortedHead;
    printf("sortedCur is %p with value %i\n",sortedCur, sortedCur->data );
    printf("sortedCur->next is %p \n",sortedCur->next );
    printf("sortedhead is %p with value %i\n",sortedHead, sortedHead->data );
    printf("sortedHead->next is %p \n",sortedHead->next );
    while((sortedCur->next!=NULL) && (sortedCur->data < cur->data)){
        sortedCur=sortedCur->next;
    }
    if(sortedCur->next == NULL && emptyflag ==0){
        sortedCur->next = cur;
        sortedCur->next = NULL;
    }

    if(sortedCur->data < cur->data && emptyflag ==0){
        sortedCur->next = cur;
        cur->next = sortedCur->next->next;
    }
    emptyflag=0;

    cur = fwd;

    printf("end of loop cur is %p\n", cur);
    printf("end of loop cur has value %i\n", cur->data);
    printf("end of loop sortedHead is %p\n", sortedHead);
    printf("end of loop sortedHead has value %i\n", sortedHead->data);
    printf("end of loop sortedHead->next is %p \n", sortedHead->next);
    printf(" -------------------------------------------------- \n");
  }

  printf("this won't print\n");
  *headpp=sortedHead;
  }

我希望循环结束,并将链接列表的开头更改为排序后的列表,以便随后调用该列表的任何函数都将生成一个排序后的列表。

2 个答案:

答案 0 :(得分:2)

如果curNULL,则此行

printf("end of loop cur has value %i\n", cur->data);

通过取消引用NULL来调用未定义的行为。然后什么都可能发生,很可能是程序崩溃了。

答案 1 :(得分:0)

@alk所述,此行可能导致分段错误。

printf("end of loop cur has value %i\n", cur->data);

将其更改为:

if (cur != NULL)
    printf("end of loop cur has value %i\n", cur->data);

您的功能正常工作。链接列表的标题更改为已排序的列表。 我使用此程序对其进行了测试:

int main(){
    // Create the head node
    struct node *head = malloc(sizeof(struct node));
    head->next = NULL;
    head->data = 10;

    // Create 9 more nodes, 10 in total
    struct node *ptr = head;
    int i;
    for (i = 9; i > 0; i--){
        ptr->next = malloc(sizeof(struct node));
        ptr->next->next = NULL;
        ptr->next->data = i;
        ptr = ptr->next;
    }

    // Print all the nodes
    ptr = head;
    while (ptr != NULL){
        printf("Node: %p, Next: %p, Data: %d\n", ptr, ptr->next, ptr->data);
        ptr = ptr->next;
    }

    LL_sort(&head);

    // Print the sorted nodes
    ptr = head;
    while (ptr != NULL){
        printf("Node: %p, Next: %p, Data: %d\n", ptr, ptr->next, ptr->data);
        ptr = ptr->next;
    }

    return 0;
}