带有链接列表的C程序最终崩溃

时间:2015-06-27 05:45:24

标签: c debugging linked-list

我正在进行一项任务,这是Boggle游戏的开始。基本前提是我们有一个包含96个字符的文本文件,我们的程序将分别读取这些字符并将它们作为节点添加到线性链表中,然后将每个项目复制到另一个线性链表中每个骰子上有6个字符,总共16个骰子。我的程序现在正常工作(部分归功于我在这里收到的反馈),但它在执行结束时崩溃了,我不知道为什么。非常感谢任何指导,因为我的编译器没有给我任何错误。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Struct for data
struct boggleDataNode
{
    char data[3];
    struct boggleDataNode *nextData;
};

// Struct for die
struct boggleDieSideNode
{
    char dieSideData[3];
    struct boggleDieSideNode *nextSide;
};

// Function to read the data file "BoggleData.txt"
void read(struct boggleDataNode **head1)
{
    char ch[3];

    FILE *input;

    input = fopen("BoggleData.txt", "r");

    if (input == NULL)
    {
        printf("Cannot open data file.\n");
    }
    else
    {
        while(fscanf(input, "%s", ch) != EOF)
        {
            addBoggleData(head1, ch);
        }
    }

    fclose(input);

    return;
}

// Function to add character from file to BoggleData Linked List
void addBoggleData(struct boggleDataNode **head1, char * ch)
{
    struct boggleDataNode *temp = NULL;
    struct boggleDataNode *right = NULL;

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

    strcpy(temp->data, ch);

    temp->nextData = NULL;

    if (*head1 == NULL)
    {
        *head1 = temp;
    }
    else
    {
        right = *head1;

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

        right->nextData = temp;
    }

    return;
}

// Function to add a node to the linked list that contains the side data for each die
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;
}

// Function to display the nodes of the linked list that contains the data from the data file
void displayDataFile(struct boggleDataNode *head1)
{
    int value = 0;
    struct boggleDataNode *temp = NULL;
    temp = (struct boggleDataNode *)malloc(sizeof(struct boggleDataNode));

    temp = head1;

    printf("**** Displaying Boggle Data ****\n");

    if(temp == NULL)
    {
        return;
    }
    else
    {
        while(temp != NULL)
        {
            printf("Data value %d : %s\n", value, temp->data);
            value++;

            temp = temp->nextData;

            if(temp == NULL)
            {
                break;
            }
        }
    }

    return;
}

// Function to display the nodes of the linked list that contains the data on the six sides of the die
void displayDieSide(struct boggleDieSideNode *head2)
{
    int value = 0;
    struct boggleDieSideNode *temp = NULL;
    temp = (struct boggleDieSideNode *)malloc(sizeof(struct boggleDieSideNode));

    temp = head2;

    if(temp == NULL)
    {
        return;
    }
    else
    {
        while(temp != NULL)
        {
            printf("Side %d : %s\n", value, temp->dieSideData);
            value++;

            temp = temp->nextSide;

            if(temp == NULL)
            {
                break;
            }
        }
    }

    return;
}

// Main function
int main()
{
    int counter = 0;
    int i = 0;

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

    read(&head1);

    displayDataFile(head1);

    for(i = 0; i < 16; i++)
    {
        head2 = NULL;

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

        printf("\n");
        printf("**** Displaying Die Side Data ****\n");
        displayDieSide(head2);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

UUgh。当你看到主要错误时,你会自杀。

for(i = 0; i < 16; i++)
{
    head2 = NULL;

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

    printf("\n");
    printf("**** Displaying Die Side Data ****\n");
    displayDieSide(head2);
}

两个循环中i发生了什么?看起来像:

for(i = 0; i < 16; i++)
{
    head2 = NULL;

    for(j = 0; j < 6; j++)
    {
        addBoggleDieSide(head1, &head2, counter);
        counter++;
    }

    printf("\n");
    printf("**** Displaying Die Side Data ****\n");
    displayDieSide(head2);
}

这只是你问题的一部分。其余的都是你可以做的改进。最重要的是不会转换malloc的结果。其余的是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Struct for data
struct boggleDataNode
{
    char data[3];
    struct boggleDataNode *nextData;
};

// Struct for die
struct boggleDieSideNode
{
    char dieSideData[3];
    struct boggleDieSideNode *nextSide;
};

// Function to add character from file to BoggleData Linked List
void addBoggleData(struct boggleDataNode **head1, char * ch)
{
    struct boggleDataNode *temp = NULL;
    struct boggleDataNode *right = NULL;

    temp = malloc (sizeof *temp);

    strcpy(temp->data, ch);

    temp->nextData = NULL;

    if (*head1 == NULL)
    {
        *head1 = temp;
    }
    else
    {
        right = *head1;

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

        right->nextData = temp;
    }

    return;
}

// Function to read the data file "BoggleData.txt"
void read(struct boggleDataNode **head1)
{
    char ch[3];

    FILE *input;

    input = fopen("BoggleData.txt", "r");

    if (input == NULL)
    {
        printf("Cannot open data file.\n");
    }
    else
    {
        while(fscanf(input, "%2[^\n]%*c", ch) != EOF)
        {
            addBoggleData(head1, ch);
        }
    }

    fclose(input);

    return;
}

// Function to add a node to the linked list that contains the side data for each die
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 = malloc(sizeof *temp);

    // helper = malloc(sizeof *helper);

    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;
}

// Function to display the nodes of the linked list that contains the data from the data file
void displayDataFile(struct boggleDataNode *head1)
{
    int value = 0;
    struct boggleDataNode *temp = NULL;
    // temp = malloc(sizeof *temp);

    temp = head1;

    printf("**** Displaying Boggle Data ****\n");

    if(temp == NULL)
    {
        return;
    }
    else
    {
        while(temp != NULL)
        {
            printf("Data value %d : %s\n", value, temp->data);
            value++;

            temp = temp->nextData;
/*
            if(temp == NULL)
            {
                break;
            }*/
        }
    }

    return;
}

// Function to display the nodes of the linked list that contains the data on the six sides of the die
void displayDieSide(struct boggleDieSideNode *head2)
{
    int value = 0;
    struct boggleDieSideNode *temp = NULL;
    // temp = malloc (sizeof *temp);

    temp = head2;

    if(temp == NULL)
    {
        return;
    }
    else
    {
        while(temp != NULL)
        {
            printf("Side %d : %s\n", value, temp->dieSideData);
            value++;

            temp = temp->nextSide;

//             if(temp == NULL)
//             {
//                 break;
//             }
        }
    }

    return;
}

// Main function
int main()
{
    int counter = 0;
    int i = 0, j = 0;

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

    read(&head1);

    displayDataFile(head1);

    for(i = 0; i < 16; i++)
    {
        head2 = NULL;

        for(j = 0; j < 6; j++)
        {
            addBoggleDieSide(head1, &head2, counter);
            counter++;
        }

        printf("\n");
        printf("**** Displaying Die Side Data ****\n");
        displayDieSide(head2);
    }

    return 0;
}

<强>输出

$ ./bin/lldicecrash
**** Displaying Boggle Data ****
Data value 0 : 1
Data value 1 : 5
Data value 2 : 4
Data value 3 : 2
Data value 4 : 1
Data value 5 : 6
Data value 6 : 2
<snip>
Data value 94 : 2
Data value 95 : 5

**** Displaying Die Side Data ****
Side 0 : 1
Side 1 : 5
Side 2 : 4
Side 3 : 2
Side 4 : 1
Side 5 : 6

**** Displaying Die Side Data ****
Side 0 : 2
Side 1 : 1
Side 2 : 2
Side 3 : 5
Side 4 : 3
Side 5 : 5

<snip 13 more die>

**** Displaying Die Side Data ****
Side 0 : 5
Side 1 : 3
Side 2 : 4
Side 3 : 5
Side 4 : 2
Side 5 : 5

注意:你真的应该将ch作为一个字符而不是字符串(并将其作为结构和整个代码中的字符处理)。这会简化一些事情。但是,这取决于你。 (可能需要进行分配)要以字符串形式阅读,您应该改进fscanf转换说明符。看看我做的改变。

另请注意:我不知道您的实际数据值是多少。我只是创建了一个可行的数据文件:

$ for i in {1..96}; do echo $((RANDOM % 6 + 1)) >> BoggleData.txt; done