2014-05-25 3 views
-4

연결 목록 (dic)을 채우려고합니다.내 연결된 목록이 채워지지 않음 C

dataPtr은 kitap.txt 파일에서 오는 단어가되고 ID은 0,1,2가됩니다.

이 문제를 해결할 수 없습니다. 잘못된 것이 있습니까? 이들과

temp=temp->next; 
temp=malloc(sizeof(NUMNODE)); 

:

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

#define WORD_LENGHT 30 

typedef struct numnode//MY STRUCTURE 
{ 
    char dataPtr[1000]; 
    int ID; 
    struct numnode* next; 

} NUMNODE; 

typedef struct num 
{ 
    NUMNODE* head; 
} NUM; 

////// 
////// 
////// 

int main() 
{ 
    char word[1000]; 
    int total=0; 
    FILE *test= fopen("kitap.txt", "r"); 

    NUM* dic; 
    dic=(NUM*)malloc(sizeof(NUM)); 
    NUMNODE* temp; 
    temp=dic->head; 
    temp=malloc(sizeof(NUMNODE)); 

    while (!feof(test))//getting all words and trying to fill linkedlist 
    { 
     fscanf (test, "%s",word); 
     strcpy(temp->dataPtr,word);//filling dataPtr part 
     temp->ID=total;//filling ID 
     printf("%d-%s\n",temp->ID,temp->dataP1tr);//i can print here but its not filling 
     total++; 
     temp=temp->next; 
     temp=malloc(sizeof(NUMNODE)); 
    } 

    //printf("-----------"); 
    FILE*file= fopen("kitap.txt", "r"); 
    //FILE*dictionary=fopen("dictionary.txt","w"); 

    temp=dic->head;//point to head 
    /*while(!feof(file))//trying to print linked list 
    { 

     printf("%d-%s\n",temp->ID,temp->dataPtr); //it only prints head node 
     temp=temp->next; 
    }  
    return; 
} 

답변

0

시도는이 두 가지 지침을 교환하는

temp->next=malloc(sizeof(NUMNODE)); 
temp=temp->next; 
+0

가이 일을하지만 난이 모든 목록 을 인쇄 목록을 인쇄하려고하지만 경우에도 별도의 의미를 인쇄 항목을 닫고 자신을 닫습니다. – user3554520

+0

목록의 마지막 항목에 temp-> next = NULL을 지정해야합니다. – mpampana

관련 문제