2011-03-25 128 views
-1

我寫了一個程序,按降序將節點插入到鏈表中。但是每當我按照這個順序測試我的代碼時,數字爲12,14,13,19,7。每當我輸入7時,我都拿到了7,已經在列表中。但是作爲在我插入之前很容易看到7不在列表中。給出這個錯誤後,如果我通過輸入2我的程序選擇了打印選項,我的程序在無限循環中輸入。我看不到我的錯誤,我很困惑。插入鏈表

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

struct node { 
    int content; 
    struct node* nextLink; 
}; 

typedef struct node NODE; 

void print (NODE*); 
int insertNode (NODE** head, int x); 

int main (void) 
{ 
    int num, choice; 
    NODE* head; 
    head = NULL; 

    do { 
     printf("\nPlease press 1 to insert or press 2 to print or press 0 to exit\n"); 
     scanf("%d", &choice); 
     switch (choice) { 
     case 0: 
      return 0; 
      break; 

     case 1: 
      printf("Enter an integer to insert into the linkedlist: "); 
      printf("\n"); 
      scanf("%d", &num); 
      insertNode(&head, num); 
      break; 

     case 2: 
      print(head); 
      break; 

     default: 
      printf("You entered an invalid number\n"); 
      return 0; 
      break; 
     } 
    } while (choice == 1 || choice == 2); 

    return 0; 
} 

int insertNode (NODE** head, int i) 
{ 
    NODE* newNode; 
    newNode   = (NODE*)malloc(sizeof(NODE)); 
    newNode->content = i; 
    NODE* temporary = *head; 
    newNode->nextLink = NULL; 

    if ((*head == NULL) || ((*head)->content) < i) { 
     *head    = newNode; 
     (*head)->nextLink = temporary; 
    } 
    else { 
     do { 
     if (((temporary->content) > i) && ((temporary->nextLink->content) < i)) { 
      newNode->nextLink = temporary->nextLink; 
      temporary->nextLink = newNode; 
      return; 
     } 
     else if (temporary->content == i) { 
      printf("To be inserted value is already in the list\n"); 
      return; 
     } 
     temporary = temporary->nextLink; 
     } while (temporary->nextLink != NULL); 

     if (temporary->content == i) { 
     printf("To be inserted value is already in the list\n"); 
     return; 
     } 

     temporary->nextLink = newNode; 
    } 
    return 0; 
} 

void print (NODE* head) 
{ 
    if (head == NULL) { 
     printf("\nLinkedList is empty \n"); 
    } 

    while (head != NULL) { 
     printf("%d ", head->content); 
     head = head->nextLink; 
    } 
} 
+2

不會編譯!!請發表正確的可編譯代碼 – Sadique 2011-03-25 21:12:57

+0

我想你想做NODE * temporary = head;而不是*頭 – Chris 2011-03-25 21:13:09

+0

不,我用那裏有一個雙指針,所以我應該這樣寫。 – virtue 2011-03-25 21:16:36

回答

0

我編譯它並運行它,它似乎工作正常,除了一件事。 insertNode定義爲返回一個int,但3個返回語句是無效返回。爲了編譯它,我將它們改爲return 0;。如果你能夠按原樣編譯並運行它,那麼它可能是由於不一致的返回而被破壞。

+0

謝謝大家。尤其是MacGucky。代碼現在可以正常工作,但是如何通過只刪除空行來糾正代碼?是否有很多空行是編譯器的問題?這個問題是給MacGucky的,因爲他編輯了我的代碼並且工作正常。 – virtue 2011-03-25 21:34:17

+0

@virtue:我看着他的編輯,我沒有看到任何會影響行爲的變化。我可能錯過了一些東西,但它看起來完全像格式更改,使代碼更具可讀性。所以我不知道問題是什麼。我是否按照我的建議改變了回報聲明?我很好奇,如果這有什麼影響。 – 2011-03-25 21:56:01

+0

@virtue:我只是通過我的代碼格式化程序(uncrustify)運行它,並手動刪除了一些空行。我編譯並測試了你的代碼和我的代碼(兩者都改變​​了所有'return;'在insertNode中改變爲'return 0;'),並且它們都按預期工作。我沒有發現任何問題。 – MacGucky 2011-03-25 22:03:10

0

如果要插入的前兩個值按降序排列,則您的代碼將不起作用。它會給分段錯誤。

對於第二個元素的插入,你需要小心

所以經過如果條件

else if (temporary->content > i && temporary->nextLink==NULL) 
     (*head)->nextLink = newNode; 
0

你的代碼是做得太多。如果以不同的方式對其進行編碼,則沒有特殊情況(例如頂部插入,插入尾部列表)。

int insertNode (NODE **head, int val) 
{ 
    NODE *newnode; 

    for (; *head; head = &(*head)->nextLink) { 
     if ((*head)->content == val) { 
      printf("To be inserted value (%d)is already in the list\n", val); 
      return 0; 
      } 
     if ((*head)->content > val) break; 
     } 
    newnode = malloc(sizeof *newnode); // Maybe check return here ;-) 
    newnode->content = val; 
    newnode->nextLink = *head; 
    *head = newnode; 
    return 1; 
}