Removing a NODE for good (double linked lists) 
Author Message
 Removing a NODE for good (double linked lists)

Quote:
>Also, in your algorithm you only re-point pointers; don't forget
>to free() the node itself, or you'll have a memory leak.
>Alex Krol

I think I'm getting the basic idea of double-linked lists but as Alex
Krol mentioned I haven't used free() on the NODE after re-pointing the
pointers.  Below seems to work and I'm hoping that it's correct but
when do you free the NODE?

while(current_ptr->next_ptr != NULL) {
        if(current_ptr->previous_ptr == NULL) {
                head_ptr = current_ptr->next_ptr;
                current_ptr->next_ptr->previous_ptr = NULL;
        } else {
                /* How do I find the NODE to free? */
                current_ptr = current_ptr->previous_ptr;
                current_ptr->next_ptr =
current_ptr->next_ptr->next_ptr;
                current_ptr->next_ptr->previous_ptr = current_ptr;
        }      
        current_ptr = current_ptr->next_ptr;

Quote:
} /* End of while */

This way of thinking is totally new to me, I find it extremely
challenging but a lot of fun.  I try to read all the articles in this
group hoping to one day understand what everbody is talking about.
Actually, I'm starting to understand more and more each day.  How much
do you need to know before trying for a job in this field?  Maybe
become a Junior programmer and enjoy what I'm doing for a livin.....
/* Wake up!!! */  Huhh?

I appreciate all the help!
Richard

/* Trees are next in my book :) */



Sun, 27 Feb 2000 03:00:00 GMT  
 Removing a NODE for good (double linked lists)

Quote:

> I think I'm getting the basic idea of double-linked lists but as Alex
> Krol mentioned I haven't used free() on the NODE after re-pointing the
> pointers.  Below seems to work and I'm hoping that it's correct but
> when do you free the NODE?

Hi Richard Zilavec,

You free a node when you no longer need it and it is no longer
referenced. So if you decide to remove a certain element from the list,
you set a pointer to the element to be removed first. Then you do the
actual removing, ie. reorder the list so does not point to the element
any more. *Afterwards* you call the "free()" function for the element.
This will make the pointer invalid, because it now points to illegal
unallocated memory.

Quote:
> This way of thinking is totally new to me, I find it extremely
> challenging but a lot of fun.

That's what I always say: programming is fun !

Quote:
> I try to read all the articles in this
> group hoping to one day understand what everbody is talking about.

That's a commendable goal, but hard to manage. There's a lot coming in
per day. For starters you can skip everything headed "How to kil MS".
You'll probably work out some more filtering rules yourself ;-)

Quote:
> Actually, I'm starting to understand more and more each day.

That's good. Have you read the FAQ yet ? It will give you a one month
dose of knowledge in a day.

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Quote:
> How much
> do you need to know before trying for a job in this field?  Maybe
> become a Junior programmer and enjoy what I'm doing for a livin.....

Really a lot. Here in Germany i took a programmer education, which
took 3 years. But with the education certificate I had no problem in
finding a job.

Quote:
> /* Trees are next in my book :) */

If you've mastered doubly linked lists, trees are a piece of cake. It's
exactly the same data structure for binary trees, simply with other
names for the node pointers, and of course with a different concept
behind the whole thing.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 27 Feb 2000 03:00:00 GMT  
 Removing a NODE for good (double linked lists)



Quote:
>while(current_ptr->next_ptr != NULL) {
>    if(current_ptr->previous_ptr == NULL) {
>            head_ptr = current_ptr->next_ptr;
>            current_ptr->next_ptr->previous_ptr = NULL;
>    } else {
>            /* How do I find the NODE to free? */
>            current_ptr = current_ptr->previous_ptr;
>            current_ptr->next_ptr = current_ptr->next_ptr->next_ptr;
>            current_ptr->next_ptr->previous_ptr = current_ptr;
>    }      
>    current_ptr = current_ptr->next_ptr;
>} /* End of while */

You'll find keeping track of this kind of thing much easier if you're
more generous in your allocation of variables.  In other words, declare
a few more pointers, instead of trying to do it all relative to
current_ptr.  E.g.

        NODE *prev_node, *next_node;

        prev_node = current_ptr->previous_ptr;
        next_node = current_ptr->next_ptr;
        /* You can free current_ptr any time between here...*/
        if (prev_node == NULL)
            head_ptr = next_ptr;
        else
            prev_node->next_ptr = next_node;
        next_node->previous_ptr = prev_node;
        /*...and here */
        current_ptr = next_node;



Sun, 27 Feb 2000 03:00:00 GMT  
 Removing a NODE for good (double linked lists)

Quote:

> Below seems to work and I'm hoping that it's correct but
> when do you free the NODE?

    Be sure to get a copy of current_ptr->next_ptr before you
    free current_ptr!

    Try this:

    Element *Head = NULL, *Tail = NULL;

    void free_element(Element *current)
        {
        /* Unlink from the left */
        if (current->previous == NULL)
            Head = current->next;
        else
            current->previous->next = current->next;

        /* Unlink from the right */
        if (current->next == NULL)
            Tail = current->previous;   /* Maintain a tail pointer */
        else
            current->next->previous = current->previous;

        /* Free the storage */
        free(current);
        }

    void free_all(void)
        {
        while (Head != NULL)
            free_element(Head);

        assert(Tail == NULL);   /* Check for a logic error */
        }

-----------------------------------------------------------------
John A. Wasser, PATHWORKS Engineering, Digital Equipment Corp.
Mailstop LKG2-1/T01, 550 King Street, Littleton MA, USA 01460

Home Page: http://www.tiac.net/users/wasser/index.html
         "I work on IBM-PC clones but I use Macintoshes."



Tue, 29 Feb 2000 03:00:00 GMT  
 Removing a NODE for good (double linked lists)



Quote:
>    Be sure to get a copy of current_ptr->next_ptr before you
>    free current_ptr!

Absolutely!  I made a similar error (I knew better, I just goofed)
in the original release of my <dirent.h> implementation (UNIX version).

In a big project a few years ago (BRL's MUVES), Karen Ross Murray
implemented a doubly-linked list package and I implemented the basic
memory allocation package.  The latter was basically a wrapper around
the native malloc/free, maintaining its own linked list of freed nodes
(of various bucket sizes) and, the point of this paragraph, providing
extensive debugging capabilities.  One thing I did to catch use of
freed storage was (in the allocator wrapper) to fill the storage with
garbage data carefully selected to be invalid pointers and unlikely
arithmetic data.  That way, we caught such usage errors early in
development, when the program trapped due to invalid memory address.

There are commercial packages that provide support along these lines,
but it's not all that hard to follow the MUVES approach yourself.



Fri, 03 Mar 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Removing node from linked list

2. nodes linked listed and such

3. 2 nodes end up in a cycle in a single linked list

4. newbie question: removal of node in linked list

5. How to find a node in link list

6. Realloc()ing data of a node from a linked list

7. why malloc for a new node in linked list

8. Destroying All Nodes in a Linked List

9. Destroying All Nodes is a Linked List

10. deleting a previous node in a singly linked list

11. Question about swapping nodes in a doubly linked list

12. Deleting a node in a linked list

 

 
Powered by phpBB® Forum Software