2014-02-14 131 views
3

我試圖創建一個程序,以升序將數字插入到鏈接列表中。這是我的插入功能。它適用於插入一些數字,但不是其他數字。我認爲這與最後一部分有關,但我無法弄清楚。C - 按升序插入鏈接列表

node* insert(node* head, int value) { 

    //check if head hasn't been created 
    if (head == NULL) { 
     head = malloc(sizeof(node)); 
     if(head == NULL) { 
      printf("Failed to create head node"); 
      return head; 
     } 
     head->value = value; 
     head->next = NULL; 
     return head; 
    } 

    //create a new node 
    node *newNode; 
    newNode = malloc(sizeof(node)); 
    if(newNode == NULL) { 
     printf("Failed to create node"); 
     return newNode; 
    } 
    newNode->value = value; 
    newNode->next = NULL; 

    //see if new node should be placed before head 
    if (value < head->value) { 
     newNode->next = head; 
     return newNode; 
    } 

    //search through to find correct spot and insert the node 
    node *temp = NULL; 
    temp = head; 
    while(temp->next != NULL && temp->value < value) { 
     temp = temp->next; 
    } 
    newNode->next = temp->next; 
    temp->next = newNode; 
    return head; 

} 
+0

您能否提供一些與預期相同的輸入以及不是哪種輸入的示例? – 2rs2ts

回答

4

部分的下列壞

//search through to find correct spot and insert the node 
node *temp = NULL; 
temp = head; 
while(temp->next != NULL && temp->value < value) { 
    temp = temp->next; 
} 
newNode->next = temp->next; 
temp->next = newNode; 

例如解決這樣的:

node *temp ,*prev; 
temp = head; 
while(temp != NULL && temp->value <= value) { 
    prev = temp; 
    temp = temp->next; 
} 
newNode->next = temp; 
prev->next = newNode; 

node *temp ,*prev; 
temp = head->next; 
prev = head; 
while(temp != NULL && temp->value < value) { 
    prev = temp; 
    temp = temp->next; 
} 
newNode->next = temp; 
prev->next = newNode; 
+0

你好,你想介紹一下代碼的最後部分嗎?我看到它會讓我有點困惑。所以基本上你將頭指針分配爲prev,並將頭指針指定爲temp。然後,對於while循環,通過鏈接列表循環並比較值,如果值較小,則插入它並更新指針節點。那麼如果這個值較大,你可以在while循環之後通過代碼添加它? – hyperfkcb

+0

是的。例如列表:{1,2,2,3,3,4},值:3(情況1){1,2,2,3,3,,4}(情況2){1,2,2, ,3,3,4}。 – BLUEPIXY

0

您需要在last while循環內檢查temp->next->value

0
//This code of mine works perfectly. 

void insertInAscOrder(int val) 
{ 
    node *new1; 
    node *temp; 
    node *previous; 

    //create new node 
    new1 = (node *)malloc(sizeof(node)); 

    //check whether node is created or not 
    if(new1 == NULL) 
    { 
     printf("Insufficient memory."); 
     return; 
    } 

    //Updating different parts of the node 
    new1 -> info = val; 
    new1 -> next = NULL;  

    //checking whether the node created is only node or not 
    if (start == NULL) 
    {  
     start = new1; 
    } 
    //If value is less than the value of first node 
    else if(val < start -> info) 
    { 
     new1 -> next = start; 
     start = new1; 
    } 
    else 
    { 
     previous = start; 
     temp = start -> next; 


      //Go to the position where node is to be inserted 
      while(temp != NULL && val > temp -> info) 
      { 
       previous = temp; 
       temp = temp -> next; 
      } 


      //Insert the node at particular position 
      if(temp == NULL) 
      { 
        previous -> next = new1; 
      } 
      else 
      { 
        new1 -> next = temp; 
       previous -> next = new1; 
      } 
    } 
} 
0

這將是好得多,如果你先執行(和測試)功能,如:(前插入)push_front()insert()push_back(), (可能是advance (Node* curr, int steps);),然後簡單考慮插入的所有可能性,即:

  • 空列表(當前節點是第一個,所以才push_front/back()
  • 迭代(advance())在從head所有元素而上,直到:用值比新的更大
    • 元素被發現,insert()之前。
    • 最後一個元素到達,push_back()

在您的新功能insert_ordered()