我希望我能在这里重新审视我的代码。我正在做一个 Boggle 游戏开始的任务。基本前提是我们有一个包含 96 个字符的文本文件,我们的程序将分别读取这些字符并将它们作为节点添加到线性链表中,然后将每个项目复制到另一个线性链表中每个骰子上有 6 个字符,总共有 16 个骰子。我已经让大多数函数正常工作,除了下面的一个,它假设采用具有所有 96 个字符的线性链表(struct boggleDataNode)并将每个字符复制到第二个线性链表(struct boggleDieSideNode)。函数中的第三个参数假设是被复制字符的索引。我在下面包含了我的主要功能,因此您可以查看实现。任何见解或指导将不胜感激,因为我现在迷路了!
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;
}