0

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);
}
3
  • have you tried using the debugger to step through? Commented Apr 1, 2013 at 1:42
  • @MitchWheat Not sure how to do that. I'm still quite new to C. Commented Apr 1, 2013 at 1:44
  • Learning to use a debugger is very useful, but in many cases it's quicker to read the code and see if it's logically doing what you want. See my answer ... no debugger needed (yet). Commented Apr 1, 2013 at 1:48

1 Answer 1

3

In your [addTo]Back function

return(current);

should be returning the head of the list instead. What you're doing is truncating the list to its last two elements.

One way to avoid such bugs is to be precise with your semantics. What is addToBack defined to return? The caller expects it to return the list, with the node added. There should be a documentation comment before the function, stating what it does and what it returns. That comment can guide you in writing the code ... and the code would be better, if there were just one statement at the end that returns the list, not several returns. Then this bug could not have happened.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks so much! That has fixed it. I've run into another issue where for some reason, the while loop might get stuck and not be able to exit?
The while loop will get stuck if you create a loop in your list. Having addToFront call addToBack and v.v. is suspicious ... neither function should call the other. addToBack should just return the node if the list is null or find the end of the list and append the node, and addToFront should just make the node's next ptr point to the list and then return the node.
Thanks for your input, I will inspect that.
to avoid the functions calling each other, I have implemented this check in the main: if(line==NULL) line2 = addToFront(line,toBeAdded); else line = addToBack(line,toBeAdded); however, now where it was once not infinitely looping, it is now.
@user2225940 Well, I explained what addToFront and addToBack should do. Main should call one or the other depending on whether you want to treat the list like a stack (addToFront) or a queue (addToBack) ... it should not be conditional on whether the list is empty.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.