2014-12-02 84 views
2

我真的可以使用一些我一直在努力的地址簿程序的幫助。我正在使用C中的雙向鏈表。我試圖在用戶輸入的位置將列表添加到列表中,從位置0開始。位置不會輸入超出範圍。 (在位置0之前沒有插入位置1之前的位置等)。位置可以重複,但是:將新節點插入到先前位置佔用者之前的位置。 (例如:如果位置1有x,並且新節點插入y位置1,則位置1現在有y,位置2有x)雙向鏈接列表C,在特定位置插入

我需要輸入用戶輸入的位置編號並檢索當前人員在那個位置,但我不能完全正確。另外,如果你想看看這個插件的功能,那麼我也包含了插入功能,因爲它也不能正常工作。謝謝你的幫助!

編輯:主要的問題,現在是我尋找pPersonCur代碼失敗時位置== 1。此外,插入函數不按正確的順序中的位置不移動的進入的東西(最新插入較舊的插入正確)。然而,破碎的pPersonCur代碼很難診斷爲什麼這是確切的。

addressbook.h摘錄:

typedef struct person Person; 
struct person { 
    char lastName[255]; 
    char firstName[255]; 
    char email[255]; 
    char phoneNumber[255]; 
    Person *pNext; 
    Person *pPrev; 
}; 

addressbook.c摘錄:

#include "addressbook.h" 

Person * InsertPerson(Person * pPersonCur) { 
    Person * pPersonNew; 

    /* data gathered for CreatePerson() function here */ 

    pPersonNew = CreatePerson(pLastName, pFirstName, pEmail, pPhoneNumber); 

    if (pPersonCur) 
    { 
     pPersonNew->pNext = pPersonCur; 
     pPersonNew->pPrev = pPersonCur->pPrev; 
     pPersonCur->pPrev = pPersonNew; 
     if (pPersonNew->pPrev) 
      pPersonNew->pPrev->pNext = pPersonNew; 
    } else 
    { 
     pPersonNew->pPrev = pFirst; 
     pPersonNew->pNext = NULL; 
     if (pFirst) 
      pFirst->pNext = pPersonNew; 
    } 
    return (pPersonNew); 
} 

main.c中摘錄:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "addressbook.h" 

Person *pFirst; /* First name in list */ 

int main(void) { 

     Person *pPersonCur = NULL; /* current Person */ 
     int bDone = 0, position = 0, counter = 0; 

     pFirst = NULL; 

    printf("Ready\n"); 

    while (!bDone) { 
     char input = getchar(); 
     switch (input) { 
     case 'a': 
      counter = 0; 
      scanf("%d", &position); /* Where desired position is entered */ 
      if (position == 0) { 
       if (pFirst) { 
        if (pFirst->pNext) { 
         pPersonCur = pFirst->pNext; 
        } 
       } else { 
        pPersonCur = pFirst; 
       } 
      } else { 
       pPersonCur = pFirst->pNext; 
       while (counter < position) { 
        pPersonCur = pPersonCur->pNext; 
        counter++; 
       } 
      } 
      InsertPerson(pPersonCur); /* Takes in person at desired position, return value is new inserted person */ 
      break; 
     /* Some other cases here */ 
     case 'q': 
      bDone = 1; 
      break; 
     } 
    } 
/* Rest of code */ 
+0

'InsertPerson'的返回值是有原因的。你可能想要利用它。 – WhozCraig 2014-12-02 21:10:54

+2

這不是一個真正的問題......它出錯了?什麼「不完全正確」? 我建議你調試它,並在整個列表中有一個手錶,然後你可能會看到它什麼時候出錯和/或被破壞,或者,*以什麼方式*它是「出錯了」! 祝你好運! – noelicus 2014-12-02 21:16:19

+0

@WhozCraig是的,我嘗試過,因爲它被用於類似的問題,但我不能想象它在這種情況下使用太多考慮到我必須明確輸入位置。 – 2014-12-02 21:25:36

回答

2

如此看來,你從來沒有分配一個值pFirst
當位置不是0時,行pPersonCur = pFirst->pNext;被執行並且pFirst在這個地方仍然是NULL

添加一個條件到你的插入函數來檢查列表的頭是否被分配。

Person * InsertPerson(Person * pPersonCur) { 
    . . . 
    else 
    { 
     pPersonNew->pPrev = pFirst; 
     pPersonNew->pNext = NULL; 
     if (pFirst) 
      pFirst->pNext = pPersonNew; 
     else 
      pFirst = pPersonNew; // If pFirst is not assigned, assign it to newly created person 
    } 
    return (pPersonNew); 
} 

儘管如此,如果你碰巧打電話InsertPersonNULL的說法,你的代碼就會把新Person後的第一個切名單關閉的其餘部分。根據位置索引

if(pFirst) { 
    Person *last = pFirst; 
    while(last->pNext != NULL) { 
     last = last->pNext; 
    } 
    last->pNext = pPersonNew; 
    pPersonNew->pPrev = last; 
} 
else 
    pFirst = pPersonNew; 

插入可能會失敗,以及如果你給一個位置:

爲了把新Person到列表的末尾時NULL叫你可以使用這樣的事情在你InsertPerson功能索引高於列表中的節點。應該添加某種安全檢查。

pPersonCur = pFirst->pNext; 
while (counter < position && pPersonCur->pNext != NULL) { // If last node reached, stop the loop 
    pPersonCur = pPersonCur->pNext; 
    counter++; 
} 

此實現將增加新Person到列表的末尾,如果位置指數過高。