I am trying to create a program which can add information to a linked list. It seems that once I add more than 2 nodes, previously added nodes get overwritten. Is there something I am missing in making this work?
I start with this function which either adds to the front of the list, or if there is already a node added to the front it calls a function 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);
}
}
The item(toBeAdded is defined by this function
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);
}