C语言 链表:尝试更改节点的索引


void changeIndex(FrameNode **head, int numOfFrames)
{
int frameIndex = 0, i = 0;
char name[STR_LEN];
FrameNode* curr = *head;
FrameNode* prev = NULL;
FrameNode* nodeToChange = NULL;
printf("Enter the name of the frame:n");
fgets(name, STR_LEN, stdin);
name[strcspn(name, "n")] = 0;
while (!(searchFrame(head, numOfFrames, name)))
{
printf("Name is not exist in the listn Please enter another name:");
fgets(name, STR_LEN, stdin);
name[strcspn(name, "n")] = 0;
}
printf("Enter the index of the frame:n");
scanf("%d", &frameIndex);
while (frameIndex > numOfFrames || frameIndex < 0)
{   
printf("Name is not exist in the listn Please enter another name:");
scanf("%d", &frameIndex);
}
getchar();
while (curr != NULL && i < numOfFrames)
{
if (strcmp(name, curr->frame->name) == 0)
{
nodeToChange = createFrame(head, numOfFrames, curr->frame->path, curr->frame->duration, curr->frame->name);
numOfFrames++;
numOfFrames = deleteFrame(head, numOfFrames, curr->frame->name);
}
curr = curr->next;
i++;
}
curr = *head;
i = 0;
if (*head != NULL)
{
while (i != frameIndex && curr->next != NULL)
{
prev = curr;
curr = curr->next;
i++;
}
prev->next = nodeToChange;
nodeToChange->next = curr;
}
else
{
*head = nodeToChange;
}
}

在这里,我尝试按名称更改节点的索引。我有一个内部有一个结构的节点。 它抛给我一个错误,说prev = NULL甚至认为我在第 4 个 while 中初始化了它(正确的值在nodeToChange中设置(。

所以我的问题是为什么会发生这种情况,你如何建议我修复它?

对于i= 0的值和frameindex=0while 循环的条件失败,因此prev已经null,您将访问引发错误的prev->next

i=0&&frameindex==0添加一个条件 并将while循环的进入条件更新为i<frameindex而不是i!=frameindex

相关内容

  • 没有找到相关文章

最新更新