gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:16:05 27 4
gpt4 key购买 nike

我正在做一项任务,该任务是 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;
}

最佳答案

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

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

关于带有链接列表的 C 程序最后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com