2016-11-21 79 views
0

我嘗試從文件中設計鏈接列表,但在設置鏈接列表的第一個節點時存在一些問題。鏈表的數據結構被示出爲下面附加的圖片: Data structure of linled list讀取文件時發生鏈接列表問題c

在我的代碼,我將兩個類型的結構,一個用於名稱,另一個用於評分,如下:

typedef struct nodeScore{ 

    int score; 
    struct nodeScore* nextScore; 

}nodeScore; 

typedef struct nodeName{ 

    char* firstName; 
    char* lastName; 

    nodeScore* nextScore; 
    struct nodeName* nextName; 

}nodeName; 

還有一個功能可以逐行讀取文件中的分數(每行包含'First name','Last name'和最多4'分數'),並返回鏈表頭部:

nodeName* storeFile(const char* fileName, nodeName* nodeN){ 

FILE *pFile = fopen(fileName, "r"); 

char input[512]; 

char* firstName; 
char* lastName; 
int score; 

int line=0; 

nodeName* prevName; 
nodeScore* prevScore; 

while(fgets(input, 512, pFile)){ 

    printf("Line %d now.\n", ++line); 

    nodeName* ptrN = (nodeName*)malloc(sizeof(nodeName));//allocate for new node name. 

    firstName = strtok(input, " ");//first space for first name. 
    ptrN->firstName = firstName; 

    lastName = strtok(NULL, " ");//second space for last name. 
    ptrN->lastName = lastName; 

    if(line == 1){ 
     prevName = ptrN; 
     nodeN = ptrN;//allocate nodeN the return value to the first line, first node of the linked list. 

    }else{ 
     prevName->nextName = ptrN; 
     prevName = ptrN; 

    } 


    ptrN->nextName = NULL; 
    ptrN->nextScore = NULL; 

    while(score = atoi(strtok(NULL, " \n"))){//store multiple scores until  next char is space or next new line. 

     if(ptrN->nextScore == NULL){//if no link to another score. 
      nodeScore* ptrS = (nodeScore*)malloc(sizeof(nodeScore));//allocate for new score node. 

      ptrN->nextScore = ptrS; 
      ptrS->score = score; 
      ptrS->nextScore = NULL; 
      prevScore = ptrS;//record latest 'tail' of linked list. 


     }else{ 
      nodeScore* ptrS = (nodeScore*)malloc(sizeof(nodeScore)); 

      prevScore->nextScore = ptrS; 
      ptrS->score = score; 
      ptrS->nextScore = NULL; 
      prevScore = ptrS;//record latest 'tail' or linked list. 

     } 


    }//done the loop for storing multi-scores. 

}//done the loop for reading lines from file. 

return nodeN; 
} 

最後主要功能:

int main(){ 
    char file1[]={"HW5_26.txt"}; 
    nodeName* n1; 

printf("\nStart reading file '%s' and store it into linked-list.\n\n", file1); 

n1 = storeFile(file1, n1); 

printf("%s", n1->firstName);//try to see who's the first person(1st node, should be Albert Einstein), here is when I'm confused. 

return 0; 
} 

在我返回值的末尾,我送花兒給人得到「莎拉」的最後一個節點的結果,但是,我已經設置了「如果過濾器」只能從第一線做出的返回值的數據,我不知道哪個部分出了問題,如果有人能給我一些建議或想法,我會很感激,謝謝。

TXT文件是這樣的:

Albert Einstein 52 67 63 
Steve Abrew 90 86 90 93 
David Nagasake 100 85 93 89 
Mike Black 81 87 81 85 
Andrew Dijkstra 90 82 95 87 
Joanne Nguyen 84 80 95 91 
Chris Walljasper 86 100 96 89 
Fred Albert 70 68 
Dennis Dudley 74 79 77 81 
Leo Rice 95 
Fred Flinstone 73 81 78 74 
Frances Dupre 82 76 79 
Dave Light 89 76 91 83 
Hua Tran 91 81 87 94 
Sarah Trapp 83 98 94 93 

我的頭:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
+1

[請參閱此討論,爲什麼不在'C'中投射'malloc()'和家族的返回值。](http://stackoverflow.com/q/605845/2173917)。 –

回答

3

的問題是您如何使用strtok,並返回指針:

firstName = strtok(input, " ");//first space for first name. 
ptrN->firstName = firstName; 

指針firstName將是一個指向數組input的指針。數組input與整個循環相同,這意味着所有名稱的所有指針都將指向相同的數組。它的也是意味着只要storeFile函數返回,那些指針就會失效,因爲該數組將不再存在,並且使用這些指針將導致未定義的行爲

有兩種可能的解決方案:一種是使用例如strdup複製字符串。另一種是在名稱結構中有數組,並複製字符串。

請注意,strdup不是一個標準的C函數,但幾乎所有的系統都有它。另外請注意,它會動態分配內存malloc,所以你需要在free之前的free這個節點。

+0

非常感謝你,它的工作原理。我學到了很多,再次感謝。祝你今天愉快! –