C - Simple Linked List program that handles strings -


the program specifications pretty simple, read in input file of words , create linked list each node contains entire string.

here's node struct:

typedef struct wordnode{     char *word;     token_type type;      struct wordnode *next; } wordnode; 

the token type come play later after ll basics working.

the general process of main open input file, set head first word, iterate through rest of input file using second node pointer, "current". afterwards, print list head pointer print list function.

void printlist(wordnode *head) {     while(head != null)     {         printf("%s ", head->word);         head = head->next;     }  }  int main() {     //open input file     file *input = fopen("input.txt", "r");     char nextword[12] = "";      //scan in first word head node     fscanf(input, "%s", nextword);      //create head, fill in first word, , create current     wordnode *head = malloc(sizeof(wordnode));     head->next = malloc(sizeof(wordnode));     head->word = nextword;      printf("%s ", head->word);     wordnode *current = head->next;  //begin iteration     while(fscanf(input, "%s", nextword) != eof)     {         current = (wordnode*)malloc(sizeof(wordnode));         current->word = nextword;         current->next = null;         printf("%s ", current->word);         current = current->next;    }      printlist(head);  } 

together, printfs within main give me output want, strings seem being saved during iteration, output given printlist function last word in list repeated couple times followed garbage values.

i'm thinking head tied current in way , doesn't stay put @ beginning of list through iteration, i'm not sure how or why moving.

also, should use strcpy saving strings nodes? tried earlier leading crashes when tried.

nextword should either larger array (e.g. nextword[200]) or should limit number of characters stored fscanf, e.g.

fscanf(input, "%11s", nextword); 

or use larger array , limit number of characters.

you need make copy of each string after reading nextword array. code assigns address of nextword array each word pointer. every node in linked list points same string, last string read file. copy string, need allocate memory , strcpy. strdup function you, strdup not supported on systems.

when code creates head allocates memory 2 structures. instead, should allocate memory 1 structure , set head->next null.

the variable current mess, e.g. set current->next = null , 2 lines later set current = current->next. make code work, need 2 variables, call them tail , current. tail should point last node in linked list. initially, tail points head. when create new node, code should this

    current = (wordnode*)malloc(sizeof(wordnode));     current->word = strdup( nextword );     current->next = null;     tail->next = current;     tail = current; 

also, don't check fscanf eof. instead, check fscanf returns expected number of conversions. reason fscanf stuck in infinite loop if conversion fails before end-of-file reached. while loop should be

while(fscanf(input, "%11s", nextword) == 1) 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -