Thread: Mass deletion from linked list

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    Mass deletion from linked list

    How would one go about deleting every single person (node) from a singly linked list which has the name Jack?

    i.e.

    Start:

    Code:
    Jack -> John -> Ann -> Bill -> Jack ->  Jack -> Frank -> Jack
    Finish:

    Code:
    John -> Ann -> Bill -> Frank

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    One way to do it:

    Create a function that deletes a single node.
    Create a function which finds a node by its name.
    Find all desired nodes by name and then delete them.

    To delete an entry in a linked list you can simply change the pointer of the previous entry to point to the next entry, then free the current entry.

    If you had:

    Jack -> John -> Ann

    Then to delete John you'd do something like:
    Code:
    temp = John;
    Jack->next = Ann;
    free( temp );

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    You want to do this faster than O(n)? Maybe you want to use hashing

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    I would like to avoid hashing. Not that I'm affraid of it, just want to do it the "normal" way using only that one linked list

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM