I have a "input.txt" :
Code:
5 //number of number follow
1 3 6 2 4
and I want to read them and create linked list for each element of sequence.
this is my code:
Code:
#include <stdio.h>
#define fi "input.txt"
typedef struct tagNode{
int info;
struct tagNode *pNext;
}Node;
typedef struct tagList{
Node *pHead;
Node *pTail;
}List;
int main(){
FILE *f;
List *nList;
nList=malloc(sizeof(List));
f=fopen(fi,"r");
int n;
fscanf(f,"%d",&n);
int i;
int c;
fscanf(f,"%d",&c);
nList->pHead->info=c;
nList->pHead->pNext=nList->pTail;
nList->pTail->pNext=NULL;
Node *tmp;
tmp=malloc(sizeof(Node));
for(i=2;i<=n;i++){
fscanf(f,"%d",&c);
tmp->info=c;
tmp->pNext=nList->pHead;
nList->pHead=tmp;
}
fclose(f);
return 0;
}
But I have got some problem at line
Code:
danhsach->pHead->info=c;
I have "segmentation fault".
So, what am I wrong here, please help me, please.
thanks