我正在尝试创建一个可以将信息添加到链接列表的程序。似乎一旦我添加了 2 个以上的节点,之前添加的节点就会被覆盖。在完成这项工作时我有什么遗漏吗?
我从这个函数开始,它要么添加到列表的前面,要么如果已经有一个节点添加到前面,它调用函数 addToBack
ListHeadPtr Front(ListHeadPtr theList, list * toBeAdded){
printf("Received in addToFront\n");
if(theList == NULL){
theList = toBeAdded;
return(theList);
}
else{
theList = addToBack(theList,toBeAdded);
/*toBeAdded->next = theList;
theList = toBeAdded;*/
return (theList);
}
}
ListHeadPtr back(ListHeadPtr theList, item * toBeAdded){
if(theList == NULL){
theList = addToFront(theList,toBeAdded);
return(theList);
}
else{
list *current;
current = theList;
while (current->next!=NULL){
current = current->next;
}
current->next = toBeAdded;
return(current);
}
}
item(toBeAdded 由该函数定义
item *createItem(int time){
item *itemPtr = (list *) malloc(sizeof(item));
if(NULL == itemPtr)
{
printf("Unable to create item\n");
return (NULL);
}
itemPtr->waitTime = time;
itemPtr->next = NULL;
return (itemPtr);
}