deleting node from linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aznblood5
    New Member
    • Mar 2010
    • 1

    deleting node from linked list

    i was wondering if anyone could help me under stands the basics of deleting a node from a linked list\

    my programs has something like this:

    typedef struct car{
    int mass;
    int x;
    int y;
    struct car *nextptr;
    struct car *lastptr;
    } car;


    and there are about 500 nodes in my linked list and i need help to find a way to deleted nodes from this linked list, more precisely, when x and y are equal for any of the nodes i want to delete the node with the smaller mass.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    How are you detecting that you have duplicates? If you can prevent it, don't add new nodes that are duplicates. When adding, check if there is a duplicate, and if so replace it!

    Otherwise:
    Assuming you have a list like
    a -> b -> c -> ... -> z

    There are several cases.
    1. Remove the head.
    - Change the head pointer from a to b,
    - delete a,
    2. Remove from the middle, say b.
    - Change the previous node's pointer from b to c
    - Remove b.
    3. Remove the last.
    - Change the previous' node's pointer from z to null.
    - Change the last node pointer from z to y.
    - Remove z.

    Assuming you can tell when two nodes are equal.

    If your concern hasn't been covered, please post back with a more specific section.

    Comment

    Working...