Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8607155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:16:45+00:00 2026-06-12T03:16:45+00:00

Can anyone check and see if there is an error with my linked-list implementation?

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:16:47+00:00Added an answer on June 12, 2026 at 3:16 am
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    struct node
    {
     node *next;
     char *command;
    };
    
    void addNode(node **listHead, char *newData)
    {
        node *newNode;
    
        if (*listHead == NULL)
        {
            *listHead = (node*)malloc(sizeof(node));
            newNode = *listHead;
        }
        else
        {
            newNode = *listHead;
            while (newNode->next != NULL)
                newNode = newNode->next;
            newNode->next = (node*)malloc(sizeof(node));
            newNode = newNode->next;
        }
        newNode->next = NULL;
        newNode->command = strdup(newData);
    }
    
    node *makeLinkedListOfWords(char *inputString)
    {
        char *token;
        char *cmdClone;
        char *delims = " ";
        node *result = NULL;
    
        cmdClone = strdup(inputString);
        token = strtok(cmdClone, delims);
        while (token != NULL)
        {
            addNode(&result, token);
            token = strtok(NULL, delims);
        }
        free(cmdClone);        // EDIT: forgot to give back the duplicate string's memory
        return result;
    }
    
    
    void printList(node *list)
    {
        int i = 0;
        while (list != NULL)
        {
            printf("%d. '%s'\n", ++i, list->command);
            list = list->next;
        }
    }
    
    int main(int argc, char *argv[])
    {
        node *list1 = makeLinkedListOfWords("this is a test");
        node *list2 = makeLinkedListOfWords("so is this");
        node *list3 = makeLinkedListOfWords("and this is another");
        printList(list1);
        printList(list2);
        printList(list3);
        return 0;
    }
    

    [EDITED: SO was turning this output into an ordered list, numbered 1-11 :grrr: )

    Output:

    1. 'this'
    2. 'is'
    3. 'a'
    4. 'test'
    1. 'so'
    2. 'is'
    3. 'this'
    1. 'and'
    2. 'this'
    3. 'is'
    4. 'another'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone help me in trying to check whether JavaScript is enabled in client
Can anyone tell me why I am getting error when I am validating even
can anyone tell me how can I check if the spinner selected or not
Does anyone how can I check whether the mailserver I use is down or
Does anyone know of a utility that can check whether a HTTP request/response is
Has anyone got a script for git that can go through the history, check
Can anyone please explain to me what this error means? Ive searched a couple
Here's my situation - lets see if any coding geniuses out there can help
We get this error in Visual Studio 2005 and TFS very often. Can anyone
Can anyone see what I'm missing? I'm using Codeigniter v1.72. In the doc: http://codeigniter.com/user_guide/libraries/form_validation.html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.