2017-03-12 61 views
0

好吧,我一直在使用scanf(cmd輸入重定向)來讀取txt文件的元素。必須爲文件中的每個條目創建一個新節點,並將其添加到列表的末尾。這裏是我到目前爲止的代碼:爲.txt文件中的每個條目創建一個新節點

struct Elem{ 
    int Atnum; 
    char Na[31]; 
    char Sym[4]; 
}; 


struct nodeTag { 
    struct Elem entry; 
    struct nodeTag *pNext; // pointer to the next node 
}; 

typedef struct nodeTag Node; 

,將其初始化函數是這樣的:

Node * 
InitializeList(Node *pFirst, int n) 
{ 
    int i; 
    Node *head, *temp = 0; 

    pFirst = 0; 

    for (i=0; i<n; i++){ 
     head = (Node *)malloc(sizeof(Node)); 
     scanf("%d", &head->entry.AtNum); 
     scanf("%s", head->entry.Na); 
     scanf("%s", head->entry.Sym); 

     if (pFirst != 0) 
     { 
      temp->pNext = head; 
      temp = head; 
     } 
     else 
     { 
      pFirst = temp = head; 
     } 
     fflush(stdin); 
     temp->pNext = 0; 
    } 

    return pFirst; 
} 

,最後,打印現在

void 
Print(Node *pFirst) 
{ 
    Node *temp; 
    temp = pFirst; 
    printf("\n status of the linked list is\n"); 
    while (temp != 0) 
    { 
     printf("%d %s %s", temp->entry.AtNum, temp->entry.Na, temp->entry.Sym); 
     temp = temp -> pNext; 
    } 

} 

,我不能讓程序正常運行。雖然沒有運行時錯誤,但輸出似乎是垃圾。我一直在爲此工作數小時,而且我無法擺弄它。感謝您的幫助!

+1

你把它們按相反順序排列,因此,你必須從你的'InitializeList()'返回* temp *,而不是* pFirst *。並且在temp-> pNext = 0;'和'pFirst = temp'行的上面給出了錯誤信息。 – 0andriy

+0

小心[使用'fflush(stdin)'](http://stackoverflow.com/questions/2979209/using-fflushstdin);除非你在Windows上,否則它不一定能做你想做的事。 –

+0

嘿,夥伴,你能給我一個關於我的回答的反饋,因爲我注意到你沒有回覆?我準備好幫助你 – UrbiJr

回答

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

struct Elem 
{ 
    int AtNum; 
    char Na[31]; 
    char Sym[4]; 
}; 

struct nodeTag 
{ 
    /* entry must be a pointer in order to not lose the values 
    and/or encounter memory conflicting errors 
    */ 
    struct Elem *entry; 
    struct nodeTag *pNext; 
}; 

typedef struct nodeTag Node; 

// insert node at the first location 
Node *insertFirst(Node *head, struct Elem *data) 
{ 

    Node *node = (Node *) malloc(sizeof(Node)); 

    // fill in data 
    node->entry = data; 

    /* point it to old first node 
     in simple words: "put this node before the head" 
    */ 
    node->pNext = head; 

    // point first to new first node 
    head = node; 

    return head; 

} 

Node *InitializeList(int n) 
{ 
    int i; 
    Node *head = NULL; 
    struct Elem *data; 

    for (i = 0; i < n; i++) 
    { 
     // allocate memory for the struct Elem of each node 
     data = (struct Elem*) malloc(sizeof(struct Elem)); 
     scanf("%d", &data->AtNum); 
     scanf("%s", data->Na); 
     scanf("%s", data->Sym); 
     head = insertFirst(head, data);   
    } 

    return head; 
} 

//display the list 
void printList(Node *head) 
{ 
    Node *ptr = head; 

    printf("\nStatus of the linked list is:\n"); 

    //start from the beginning 
    while(ptr != NULL) 
    { 
     printf("%d %s %s", ptr->entry->AtNum, ptr->entry->Na, ptr->entry->Sym); 
     printf("\n"); 
     ptr = ptr->pNext; 
    } 

} 

int main(int argc, char *argv[]) 
{ 
    Node *head; 

    head = InitializeList(3); 
    printList(head); 

    return 0; 
} 

我希望我沒來得太晚!如果不是,請檢查此答案作爲解決方案,謝謝! :-)

相關問題