【发布时间】:2010-07-20 03:07:43
【问题描述】:
我一直在使用双向链表。除了应该在“whereX”之前添加“who”副本的函数之外,一切正常[参见下面的代码]。为什么这个功能不起作用?
void addNodeAt(Node *whereX, Node *who)
{
//copy
Node *temp = (Node*)malloc(sizeof(Node));
temp->count = who->count;
strcpy(temp->word,who->word);
temp->before = whereX->before;
temp->after = whereX;
//paste
if(whereX->after == who)
whereX->after = who->after;
whereX->before = temp;
}
编辑:
回应 user326404 说:
'注意:你的函数确实存在一个缺陷,它无法将 who 作为列表的新头插入。它会插入,但您永远不会返回新的头节点,因此列表会丢失。'
如果我有一个 Node *head 作为全局变量会怎样。我怎样才能在不退回头部的情况下重新签名?
【问题讨论】:
标签: c linked-list