Search code examples
clinked-listboggle

C Linked List - Boggle Program


I was hoping I could get a fresh set of eyes on my code here. I'm working on an assignment that is the beginning of a game of Boggle. The basic premise is we have a text file with 96 characters in it, and our program is going to read in those characters separately and add them as nodes in to a linear linked list and then copy each item to another linear linked list that will place 6 characters on each die, which will total to 16 dice. I've got most of the functions working properly, except for the one below, which is suppose to take the linear linked list that has all 96 characters (struct boggleDataNode) and copy each character to the second linear linked list (struct boggleDieSideNode). The third parameter in the function is suppose to be the index of the character being copied over. I have included my main function below so you can view the implementation. Any insight or guidance would be greatly appreciated, as I'm lost currently!

void addBoggleDieSide(struct boggleDataNode *head1, struct boggleDieSideNode *head2, int index)
{
    int i = 0;

    struct boggleDieSideNode *temp = NULL;
    struct boggleDieSideNode *right = NULL;

    struct boggleDataNode *helper = NULL;

    temp = (struct boggleDieSideNode *)malloc(sizeof(struct boggleDieSideNode));

    helper = (struct boggleDataNode *)malloc(sizeof(struct boggleDataNode));

    helper = head1;

    for(i = 0; i <= index; i++)
    {
        helper = helper->nextData;
    }

    strcpy(temp->dieSideData, helper->data);

    temp->nextSide = NULL;

    if (head2 == NULL)
    {
        head2 = temp;
    }
    else
    {
        right = head2;

        while(right->nextSide != NULL)
        {
            right = right->nextSide;
        }

        right->nextSide = temp;
     }

     return;
 }







int main()
{
    int counter = 0;
    int i = 1;

    struct boggleDataNode *head1 = NULL;
    struct boggleDieSideNode *head2 = NULL;

    // Reads in original text file to boggleDataNode linked list
    read(&head1);

    // Displays boggleDataNode linked list
    displayDataFile(head1);

    for(i = 1; i <= 16; i++)
    {
        // Clears die that was just created in loop and starts a new die
        head2 = NULL;

        for(i = 1; i <= 6; i++)
        {
            addBoggleDieSide(head1, head2, counter);
            counter++;
        }

        // Displays values on each die
        displayDieSide(head2);
     }

     return 0;
}

Solution

  • The (head2 == NULL) case does not do what you are intending. head2 = temp only sets the local value of head2. As soon as the function returns that value is lost. The caller's head2 is not set and hence it will always be NULL.

    Your function should pass in a pointer to the head pointer. Something like:

    void addBoggleDieSide(struct boggleDataNode *head1, struct boggleDieSideNode **head2, int index)
    {
       ...
       if (*head2 == NULL)
       {
           *head2 = temp;
       }
       ...
    }
    
    main()
    {
        ...
        addBoggleDieSide(head1, &head2, counter);
        ...
    }