【发布时间】:2019-12-11 13:50:55
【问题描述】:
试图在链表中的某个位置之后插入一个节点,但它在之前插入了它。 尝试修改它,它仍然在之前添加它或者只是删除链表的其余部分
Destination *insertAfter(Destination *head, Destination *node, char *key)
{
Destination *ptr;
Destination *previous = NULL;
Destination *newNode = (Destination*)malloc(sizeof(Destination));
if(head == NULL)
{
return node;
}
for(ptr =head; ptr != NULL; ptr = ptr-> next)
{
if(strcmp(ptr->code, key) == 0)
{
node->next = ptr;
if(previous != NULL)
{
previous->next = node;
return head;
}
else
{
return node;
}
}
previous = ptr;
}
previous->next = node;
return head;
}
【问题讨论】:
-
@HenriPrudhomme 所以本质上是用户输入一个字符串,然后搜索链表并在他们输入的字符串之后输入
-
我有答案了,给我15分钟打出来
标签: c linked-list nodes singly-linked-list