【问题标题】:Code after while loop terminates does not runwhile循环终止后的代码不运行
【发布时间】:2019-05-19 09:57:52
【问题描述】:

我正在编写一个对链表进行排序的算法。但是,我遇到了我的 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;
  }

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

【问题讨论】:

  • 您是否尝试过使用调试器(例如 gdb)单步执行它?如果不是,这将是学习的好时机;然后你可以确切地看到什么时候执行。确保使用-O0 -g 编译您的代码,这将有助于更轻松地进行调试。
  • 另外,您得到的输出究竟是什么?链表是如何初始化的?
  • 我得到了所有的打印输出,直到 while 循环的最后一次迭代。它甚至运行 printf("end of loop cur is %p\n", cur);这告诉我 cur 是 0000000,我假设它是 NULL。我假设一旦 cur 为 NULL,循环就会结束,但是这行代码仍然运行。

标签: c sorting while-loop


【解决方案1】:

如果curNULL 那么这一行

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

通过取消引用NULL 来调用未定义的行为。那时任何事情都可能发生,很可能程序刚刚崩溃。

【讨论】:

  • 这很可能也是如此,尽管 OP 并没有对其程序的其余部分发生什么。我最终会假设它是段错误或其他东西。
【解决方案2】:

正如@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;
}

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    相关资源
    最近更新 更多