gpt4 book ai didi

c - 重新定位单链表中的节点会导致无限循环

转载 作者:行者123 更新时间:2023-11-30 16:59:48 25 4
gpt4 key购买 nike

在我的作业中,我被要求将特定节点的名称字段重新定位到特定索引。我很难弄清楚为什么我的列表变成了无限列表。问题出在函数“changePlacement”中
示例:
给定链表:1->2->3->4
输出:1->3->3->3。 ..
预期输出:1->3->2->4

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

struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
};
typedef struct Frame frame_t;

struct Link
{
frame_t *frame;
struct Link *next;
};
typedef struct Link link_t;

int listLen = 0;
link_t* lastNode = NULL;

void addFrame(link_t** list);
void frameData(frame_t* frame);
void freeVideo(link_t* video);
void showList(link_t* list);
void freeNode(link_t* list);
void changePlacement(link_t** list, int newIndex, char* name);

int main(void)
{
link_t* video = NULL;
lastNode = video;
if (!video)
{
perror(video);
}
addFrame(&video);
addFrame(&video);
addFrame(&video);
addFrame(&video);

showList(video);
changePlacement(&video, 2, "3");
showList(video);
printf("\n");
freeVideo(&video);

_flushall();
getchar();
return 0;
}



void addFrame( link_t** video)
{
char strTemp[200] = { 0 };

link_t* newFrame = (link_t*) calloc(1, sizeof(link_t));
newFrame->frame = (frame_t*) malloc(sizeof(frame_t));

printf(" *** Creating new frame ***\n");
printf("Please insert a frame path:\n");
scanf(" %s", &strTemp);
newFrame->frame->path = (char*) calloc(strnlen(strTemp, 200) + 1, sizeof(char));
strncpy(newFrame->frame->path, strTemp, strnlen(strTemp, 200));

printf("Please insert frame duration(in miliseconds):\n");
scanf(" %d", &(newFrame->frame->duration));

printf("Please choose a name for that frame:\n");
scanf(" %s", &strTemp);
newFrame->frame->name = (char*) calloc(strnlen(strTemp, 200) + 1, sizeof(char));
strncpy(newFrame->frame->name, strTemp, strnlen(strTemp, 200));

if (!(*video))
{
(*video) = newFrame;
}
else
{
lastNode->next = newFrame;
}
lastNode = newFrame;


++listLen;
}




void freeVideo(link_t** video)
{
link_t* currNode = NULL;
int loop = 0;

for (; currNode; currNode = *video) // set curr to head, stop if list empty.
{
// advance head to next element.
freeNode(currNode);
(*video) = (*video)->next; // delete saved pointer.
}
freeNode((*video)->next);
freeNode((*video));


}


void showList(link_t* list)
{
printf("\n\t|Name\t\t| Duration | Path\n");
printf("\t+++++++++++++++++++++++++++++++++++++++++++++++\n");
for (;list; list = list->next)
{
printf("\t|%10s\t|%10d\t|\t%s\n", list->frame->name, list->frame->duration, list->frame->path);
printf("\t+++++++++++++++++++++++++++++++++++++++++++++++\n");
}
}

void freeNode(link_t* node)
{
free(node->frame->name);
free(node->frame->path);
free(node->frame);
free(node);
}

void changePlacement(link_t** list, int newIndex, char* name)
{
link_t *prevOldNode = (*list), *prevTemp = NULL, *oldNode = NULL, *newNode = NULL;
link_t *temp = (*list), *PrevNewNode = NULL, *nodeAfterNewNode = NULL;
int currIndex = 0, i = 0, flag = 0;

while ((temp->next))
{
++currIndex;
if (!strcmp(temp->frame->name, name)) //looking for the wanted node
{
oldNode = temp;
prevOldNode = prevTemp;
}
if (currIndex == newIndex)
{
PrevNewNode = prevTemp;
newNode = temp;
}
prevTemp = temp;
temp = temp->next;
}
nodeAfterNewNode = newNode->next; //temporal variable that stores the node after the new node
prevOldNode->next = oldNode->next;
newNode->next = oldNode;

oldNode->next = nodeAfterNewNode;
}

看完我的代码你有什么想法吗?任何帮助都会受到祝福

最佳答案

您在 changePlacement() 末尾的代码没有考虑 oldNodenewNode 彼此相邻的情况,我。 e. e. G。 nodeAfterNewNode = newNode->nextoldNode 相同。 (此外,它没有考虑找不到名称的情况。)仅交换框架指针要容易得多;改变

    nodeAfterNewNode = newNode->next; //temporal variable that stores the node after the new node
prevOldNode->next = oldNode->next;
newNode->next = oldNode;

oldNode->next = nodeAfterNewNode;

    if (!oldNode) return;   // wanted node name not found
frame_t *f = oldNode->frame;
oldNode->frame = newNode->frame;
newNode->frame = f;

关于c - 重新定位单链表中的节点会导致无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37977119/

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