2017-08-04 53 views
0

新手問題。我有這個功能,釋放前一個節點後添加節點時程序崩潰

void removeNodes(Node *start) 
{ 
    Node *temp; 
    int counter = 0; 

    while(start) 
    { 
     temp = start; 
     start = start->next; 

     free(temp); 
     counter++; 
    } 

    printf("%d node/s has been removed\n\n", counter); 
} 

在我的main(),我有選擇。

  1. 添加節點
  2. 刪除所有節點
  3. 退出程序

有第一套環的沒有問題,直到我決定免費,我只是選項創建的節點2.問題從這裏開始。釋放它們之後,我想添加另一組節點,在我再次進入第一個節點後程序崩潰。我做的第一件事是將我的main()中的start重置爲NULL。但它仍然打破了這個計劃。

我正在閱讀的這本書沒有解釋爲什麼會發生這種情況。或者,也許他們擁有它,但他們已經在最後一頁。我在第340/632頁=)..這裏是新手。也許我只是失去了一些對其他老手來說非常簡單的東西。請幫助我。 tnx ..

順便說一下,這只是我的main()函數的一個示例。我沒有包括所有。

Node *start = NULL; 
    int choice; 
    int pos = 1; 
    int data; 
    int node_qty = 0; 

    while(1) 
    { 
     printf("1. Add node\n2. Delete all nodes\n3. Quit "); 
     scanf(" %d", &choice); 

     if(choice == 1) 
     { 
      if(!node_qty) 
      { 
       printf("Enter the value of the first node: "); 
       scanf(" %d", &data); 
      } 
      else 
      { 
       printf("Enter a value: "); 
       scanf(" %d", &data); 

       do 
       { 
        printf("Enter the position: "); 
        scanf(" %d", &pos); 

        if(pos > node_qty + 1) 
        { 
         printf("Invalid input. Current node quantity: %d\n", node_qty); 
         printf("\n\n\n\n\n\n\n\n\n"); 
         system("PAUSE"); 
         system("cls"); 
        } 
       }while(pos < 1 || pos > node_qty + 1); 
      } 

      insertNode(&start, pos, data); 

      printf("\n\n\n\n\n\n\n\n\n"); 
      system("PAUSE"); 
      system("cls"); 

      node_qty++; 
     } 
     else if(choice == 2) 
     { 
      removeNodes(start); 

      //reset 
      start = NULL; 
      node_qty = 0; 

      printf("\n\n\n\n\n\n\n\n\n"); 
      system("PAUSE"); 
      system("cls"); 
     } 
     else if(choice == 3) 
     { 
      printf("Program ends"); 
      break; 
     } 
    } 

    //then after all of that, I am making sure that it will free the nodes 
    removeNodes(start); //I don't think the problem is here. 

這是我的加點功能

void insertNode(Node **start, int pos, int data) 
{ 
    Node *temp1 = malloc(sizeof(Node)); 
    temp1->data = data; 

    Node *temp2 = *start; 

    if(pos == 1) 
    { 
     temp1->next = *start; 
     *start = temp1; 
    } 
    else 
    { 
     for(int i = 0; i < pos - 2; i++) 
     { 
      temp2 = temp2->next; 
     } 

     temp1->next = temp2->next; 
     temp2->next = temp1; 
    } 
} 

//and for checking, here's my print function 
void printNode(Node *start) 
{ 
    while(start) 
    { 
     printf("%d ", start->data); 

     start = start->next; 
    } 

    printf("\n\n"); 
} 
+1

您需要發佈剩餘的代碼。我懷疑你有一些問題填充列表。 –

+0

也許問題在別處,它不是一個正確形成的鏈表。或者也許是別的。也許你應該顯示處理選項的代碼? – lurker

+0

你應該發佈一個完整的最小和可驗證的例子。 –

回答

0

您正在訪問start當它是NULL,如果你刪除所有節點在你的程序後立即創建一個新的節點。 您應該在insertNode()的中找到一種方法來防止在start上使用->運算符。

你可以創建temp1這是新節點之後添加

if(*start == NULL) 
{ 
    *start = temp1; 
    return; 
} 

。 當列表爲空時,只有第一個位置可用於插入,因此pos的值不相關。


另一件事是PeterJ指出,添加在列表的末尾新節點的nextinsertNode()功能做出NULL
如果沒有這個功能,在使用printNode()函數遍歷列表時,無法確定何時到達最後。

因此,在insertNode()創建temp之後立即添加temp1->next = NULL;