Thread: delete item from linked list

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    delete item from linked list

    I have written the below code to remove a node from a single linked list. Now need some help to get farther. What am I missing?
    [code]
    int deleteItem(struct node** headRef, int item)
    {
    struct node* current = *headRef;
    struct node* tmp;
    int count = 0;
    if(current == NULL)
    return 1;
    if((item-1) == 0)
    {
    tmp = current->next;
    free(current);
    *headRef = tmp;
    }
    else
    {
    while(count < item-1)
    {
    tmp = current->next->next;
    current = current->next;
    free(current);
    *headRef = tmp->next;
    count++;
    }
    }
    return 0;
    }
    Code:
    int deleteItem(struct node** headRef, int item)
    {
       struct node* current = *headRef;
       struct node* tmp;
       int count = 0;
       if(current == NULL)
          return 1;
       if((item-1) == 0)
       {
          tmp = current->next;
          free(current);
          *headRef = tmp;
       }
       else
       {
          while(count < item-1)
          {
             tmp = current->next->next;
             current = current->next;
             free(current);
             *headRef = tmp->next;
             count++;
          }
       }
       return 0;
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I think you should compare items instead of counting like you do. Something like this
    Code:
    int deleteItem(struct node **headRef, int item)
    {
      struct node *current = *headRef;
      struct node *tmp;
    
      if (current == NULL) /* No list */
        return 1;
      else if (current->item == item) /* Delete first */
      {
        tmp = current;
        *headRef = current->next;
        free(tmp);
      }
      else
      {
        while (current->next != NULL)
        {
          if (current->next->item == item)
            break;
    
          current = current->next;
        }
    
        tmp = current->next;
        current->next = current->next->next;
        free(tmp;
      }
    
      return 0;
    }
    I think that would do it, single linked lists are funky.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    You need to make sure your code works for deletion at the beginning, middle and end of an SLL.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM