removing link.

This is a discussion on removing link. within the C Programming forums, part of the General Programming Boards category; I can't remember how I removed things from linked list, here is what I am getting at. get to link ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    removing link.

    I can't remember how I removed things from linked list, here is what I am getting at.

    get to link to remove
    copy everything from the next link into current link, and then advance and repeat until reach end.

    if anyone can help refresh memory, that would be greatful

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    node * detach(node * n)
    {
     n->next->prev = n->prev;
     n->prev->next = n->next;
     n->next = n->prev =  0;
     return n;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You will also want to free() the memory allocated for the struct you are deleting.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    2
    before n->next = n->prev = 0;
    you shuld do this:
    free(n);

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Well, copying bytes of memory is what the linked list is intended to avoid.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    what do you mean by this?
    >>Well, copying bytes of memory is what the linked list is intended to avoid.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    He means that linked lists are designed to be non-contiguous, to stop having to repeatedly realloc etc, stop moving data around memory.

    You should link and unlink by pointer, not move data around, it defeats the purpose a little and it's not efficient.

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    yeah, I realized that when I saw the 1st reply.
    Last edited by chrismiceli; 08-17-2003 at 10:54 AM.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Actually, the code I posted is faulty and should be:

    node * detach(node * n)
    {
    if(n->next)n->next->prev = n->prev;
    if(n->prev)n->prev->next = n->next;
    n->next = n->prev = 0;
    return n;
    }


    Originally posted by paolo_maldini
    before n->next = n->prev = 0;
    you shuld do this:
    free(n);

    Not quite. After you free() an object, you should consider it off-limits to any further accesses.

    Anyway, a detach() function is mainly for rearranging nodes, though it could be used for erasing nodes too.

    A more efficient approach might be:

    Code:
    node * detach(node * begin, node * last)
    {
     if(begin->prev)begin->prev->next = last->next;
     if(last->next)last->next->prev = begin->prev;
     last->next = begin->prev =  0;
     return begin;
    }
    
    
    node * detach_node(node * n)
    {
     return detach(n, n);
    }
    
    
    void erase(node * begin, node * last)
    {
     node * seg = detach(begin, last);
     node * goner = seg;
     while(seg)
     {
      goner = seg;
      seg = seg->next;
      free(goner);
     }
    }
    
    
    node * head(node * n)
    {
     node * first = n;
     while(first->prev) first = first->prev;
     return first; 
    }
    
    
    node * tail(node * n)
    {
     node * last = n;
     while(last->next) last = last->next;
     return last; 
    }
    
    // free the entire list from any node // 
    
    void free_list(node * anywhere)
    {
     erase(head(anywhere), tail(anywhere));
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Some further reading for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 05:23 AM
  2. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  3. Removing link libraries in static libs
    By 39ster in forum C Programming
    Replies: 1
    Last Post: 06-10-2008, 10:22 AM
  4. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21