0

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;
            }
        }
    }
}
anas munir
  • 182
  • 3
  • 10
  • There are many problems with the code, but the two major being that you loose your head node, and that you dereference a `NULL` pointer. What do you think `head->next` will be after you "traverse till end of list"? – Some programmer dude Apr 25 '16 at 12:48
  • Possible duplicate of [Definitive List of Common Reasons for Segmentation Faults](http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults) – Dan Cornilescu Apr 25 '16 at 13:07
  • Yes Joachim Pileborg, i took a break than dry ran my code, and it made absolutely no sense. I have no idea what i was thinking when i made it. But I've fixed it. Thank you for replying though. – anas munir Apr 25 '16 at 15:39

1 Answers1

0

I took a break and dry ran my code, and it made no sense. Linked list was messed up, I don't know what is thinking when i made it. But i've fixed it and it's working

    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdbool.h>
    /*the CFG
      S>ABe
      A>dB|eS|e
      B>AS|b
    */
    typedef struct node {
        //int val;
        struct node * next;
        char arr[20];//the array to save CFG per line
        }node_t;
    int main() {
        node_t * head = malloc(sizeof(node_t));
        node_t * current = malloc(sizeof(node_t));
        node_t * root = malloc(sizeof(node_t));
        head = current;

        FILE *fp;
        char c, temp; int i; bool flag = true;
        fp = fopen("cfg.txt", "r");
        while((c = fgetc(fp)) != EOF) {
            if(c == '\n') {
              current->next = malloc(sizeof(node_t));
              //current->next->next = NULL;
              current = current->next;
              current->next = NULL;  
              flag = true;
            }else if(c == '>'){continue;}
            else if(c == '|'){
                current->next = malloc(sizeof(node_t));
                current = current->next;
                i = 0;
                current->arr[i] = temp;
                i++;
                continue;
            }
             else {
                current->arr[i] = c;
                i++;
                current->next = NULL;
                if(flag){
                    tmp = c;
                    flag = false;
                }
               // continue;
            }
        }
        root = head;
        while(root->next != NULL){
            for(i=0; i<20; i++) {
                printf("%c", root->arr[i]);
            }
            printf("\n");
            root = root->next;
        }
        fclose(fp);
    }
anas munir
  • 182
  • 3
  • 10