Can anyone check and see if there is an error with my linked-list implementation? I keep getting a seg fault while trying to iterate through the linked list.
I'm trying to iterate through the linked list from the "root" in "process_command" but when I try to access root->child, I'll get a seg fault error.
Implementation of the node
typedef struct _node {
struct _node *child;
char *command;
} Command_list;
The two functions that I'm using to tokenize a string and put them into a linked-list.
Command_list *process_command( char command_line[256] )
{
printf("Command: %s", command_line);
//allocate space for root & child
Command_list *root = (Command_list*)malloc(sizeof (Command_list));
Command_list *child = (Command_list*)malloc(sizeof (Command_list));
char *token;
char *saveptr;
//get the first part of the string
token = strtok_r( command_line, " ", &saveptr);
//if the first word in the string is student
if( !strcmp(token, "student") )
{
//set the first word to the be root
root = insert_command( token, root );
printf("Current root command: %s \n", root->command);
child = root;
//get the next word from the string
token = strtok_r( NULL, " ", &saveptr);
//keep getting words from the list and store them in a linked-list
while( token != NULL )
{
child = insert_command( token, child );
token = strtok_r( NULL, " ", &saveptr);
}
}
return root;
}
Command_list *insert_command( char *value, Command_list *root)
{
printf("previous value: %s \n", root->command);
Command_list *child_node = (Command_list*)malloc(sizeof (Command_list));
//if the node being passed in is empty, store the value in itself
if( root->command == NULL ){
root->command = value;
root->child = 0;
printf("Inserting value to root: %s \n", root->command);
return root;
}
//otherwise store the value in a child node
else
{
child_node->command = value;
child_node->child = 0;
printf("Inserting value to child node: %s \n", child_node->command);
return child_node;
}
}
EDIT: Iteration code
{
....
Command_list *temp = (Command_list*)malloc(sizeof (Command_list));
temp = root;
while(temp != NULL){
printf("Command: %s\n", temp->command);
temp = temp->child;
....
}
Added the iteration code that I'm using. The code seems to work fine in code-blocks but it stops iterating after the first output in the terminal.