help with doubly linked list
Author Message
help with doubly linked list

linked list. I did not see this question, as part of FAQ. Hence, appreciate
your help.

Thanks,

Sam

Sat, 22 Feb 1997 07:43:31 GMT
help with doubly linked list
Here's an example, sort of:

struct DLinkList {
struct DLinkList *prev, *next;
DATA dat;

Quote:
};

void add(DATA *dat, struct DLinkList *Before, struct DLinkList *After) {
/* function adds a node with dat between Before and After */
struct DLinkList *A;

A = malloc(sizeof(struct DLinkList));
A->dat = *dat;
A->next = After;
A->prev = Before;

Before->next = A;
After->prev = A;

Quote:
}

void remove(struct DLinkList *Node) {
/* function removes a node */
struct DLinkList *next, *prev;

prev = Node->prev;
next = Node->next;

prev->next = next;
next->prev = prev;

free(Node);

Quote:
}

That should deal with the doubly linked lists as I interpret your question.
Don't forget to free any memory allocated to pointers within DATA. Note that
the only reason why add gets a poointer to DATA is because I'm expecting a
large data structure. Smaller ones may be better off to pass directly, but
that gets into machine dependent optimizations.

Hank

Sun, 23 Feb 1997 23:29:15 GMT
help with doubly linked list

Quote:
> Here's an example, sort of:

> struct DLinkList {
>   struct DLinkList *prev, *next;
>   DATA dat;
> };

> void add(DATA *dat, struct DLinkList *Before, struct DLinkList *After) {
> /* function adds a node with dat between Before and After */
>   struct DLinkList *A;

>   A = malloc(sizeof(struct DLinkList));
>   A->dat = *dat;
>   A->next = After;
>   A->prev = Before;

>   Before->next = A;
>   After->prev = A;

What if Before is NULL? What if After is NULL?

Quote:
> }

> void remove(struct DLinkList *Node) {
> /* function removes a node */
>   struct DLinkList *next, *prev;

>   prev = Node->prev;
>   next = Node->next;

>   prev->next = next;
>   next->prev = prev;

What if prev is NULL? What if next is NULL?

Quote:
>   free(Node);
> }

If you assume that the list consists initially of one dummy element whose
prev and next fields point to itself (which would make your solution work)
you had better mention that. It might help.

Quote:
> That should deal with the doubly linked lists as I interpret your question.
> Don't forget to free any memory allocated to pointers within DATA. Note that
> the only reason why add gets a poointer to DATA is because I'm expecting a
> large data structure. Smaller ones may be better off to pass directly, but
> that gets into machine dependent optimizations.

--
^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

There are no data that cannot be plotted on a straight line if the axis
are chosen correctly.

Mon, 24 Feb 1997 17:04:53 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages

Powered by phpBB® Forum Software