my work is to read a CFG from a file and save it in a linked list. Then traverse the linked list to remove null productions. My logic is to use an array inside the linked list. A single linked list node will point to an array in it's value part. The array will save a line of CFG until a new line. When a '\n' comes then a new node will be created and and will point to an array. The process will repeat until EOF. I've codded it but getting segmentation fault
/*the CFG
S>ABe
A>dB
B>AS
*/
typedef struct node {
//int val;
struct node * next;
char arr[5];//the array to save CFG per line
}node_t;
int main() {
node_t * head = NULL;
head = malloc(sizeof(node_t));
FILE *fp;
char c; int i;
fp = fopen("cfg.txt", "r");
while((c = fgetc(fp)) != EOF) {
head->next = malloc(sizeof(node_t));
while(head->next != NULL) { //traverse till end of list
head = head->next;
}
//now adding new line;
for(i=0; i<5; i++) {
head->next->arr[i] = c;
if(c == '>'){
continue;
}else if(c == '\n') {
break;
}
}
}
}