我正在尝试填充链接列表(字典)。dataPtr
是来自kitap.txt文件的单词,ID
依次是0,1,2。
我无法解决此问题,您能看到任何错误吗?
#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;
}
最佳答案
尝试交换以下两个说明:
temp=temp->next;
temp=malloc(sizeof(NUMNODE));
用这些:
temp->next=malloc(sizeof(NUMNODE));
temp=temp->next;
关于c - 我的链表未填写C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23854254/