简体   繁体   English

C链表-Boggle程序

[英]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. 我正在做的任务是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. 基本前提是我们有一个包含96个字符的文本文件,我们的程序将分别读取这些字符并将它们作为节点添加到线性链接列表中,然后将每个项目复制到另一个将放置的线性链接列表中每个骰子上6个字符,总共16个骰子。 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). 除了下面的一个功能外,我大多数功能都可以正常工作,该功能假定采用具有所有96个字符的线性链接列表(结构boggleDataNode)并将每个字符复制到第二个线性链接列表(结构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;
}

The (head2 == NULL) case does not do what you are intending. (head2 == NULL)情况不符合您的(head2 == NULL) head2 = temp only sets the local value of head2 . head2 = temp仅设置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. 呼叫者的head2未设置,因此它将始终为NULL。

Your function should pass in a pointer to the head pointer. 您的函数应该将指针传递给head指针。 Something like: 就像是:

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

main()
{
    ...
    addBoggleDieSide(head1, &head2, counter);
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM