[prev] [index] [next]

List Operations (cont)

  • A new node can be inserted at the end of a list:

NodeT *insertTail(NodeT *head, int d) {
   // create new node with data
   NodeT *new = makeNode(d);
   if (head != NULL) {            // check if at least 1 node
      NodeT *cur;
      cur = head;                 // start at the head
      while (cur->next != NULL) { // this must be defined
         cur = cur->next;
      }
      cur->next = new;            // cur->next was NULL
   } else {
      head = new;                 // if no list, list = new
   }
   return head;
}