|
《Windows游戏编程大师技巧》(第二版)第11章(5) NODE_PTR curr_ptr = head, // used to search the list prev_ptr = head; // previous record // test if there is a linked list to delete from if (!head) return(-1); // traverse the list and find node to delete while(curr_ptr->id != id && curr_ptr) { // save this position prev_ptr = curr_ptr; curr_ptr = curr_ptr->next; } // end while // at this point we have found the node // or the end of the list if (curr_ptr == NULL) return(-1); // couldn't find record // record was found, so delete it, but be careful, // need to test cases // case 1: one element if (head==tail) { // delete node free(head); // fix up pointers head=tail=NULL; // return id of deleted node return(id); } // end if else // case 2: front of list if (curr_ptr == head) { // move head to next node head=head->next; // delete the node free(curr_ptr); // return id of deleted node return(id); } // end if else // case 3: end of list if (curr_ptr == tail) { // fix up previous pointer to point to null prev_ptr->next = NULL; // delete the last node free(curr_ptr); // point tail to previous node tail = prev_ptr; // return id of deleted node return(id); } // end if else // case 4: node is in middle of list { // connect the previous node to the next node
|