Thread: deleting node in linked list

  1. #1
    Registered User
    Join Date
    Apr 2008

    Question deleting node in linked list

    hi i have the following code to delete a node from my linked list but it seems that theres something wrong with it. can anyone assist me with this?

    /* function to delete a node on list */
    void free_used_node(int id) {
        struct List *list, *temp;
        list =ListHead;  /* points to the 1st node of the list */
        /* handles situation if header node id is what we want to delete. */
        if(list->id == id) {
            ListHead = list->next;
        /* if not, loop thru list and find out target */
        while(list != NULL) {
            if(list->next) {
                if(list->next->id == id) {
                    temp = list->next;
                    list->next = temp->next;
            list = list->next;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    It would help if you described the "doesn't seem to work" in a bit more detail: What is not working.

    Here's my thoughts:
    Well, once you have deleted something out of the list, I guess you shouldn't need to search any further (or is the ID not unique?)

    As a consequence of the above, you are also potentially accessing a "dead" node, because list is still pointing to the deleted item when you have deleted the first node.

    I also suspect you can't delete the final node of the list - but without knowing exactly what your list looks like.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Problems with deleting a node in a singly linked list
    By omishompi in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2006, 06:27 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM