lkml.org 
[lkml]   [2000]   [Feb]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: Improved <linux/lists.h>
On Tue, Feb 22, 2000 at 07:02:34PM +0100, Andreas Gruenbacher wrote:
> Borislav Deianov wrote:
> > AFAIK, linux/list.h is the recommended linked list implementation,
> > consider using that instead.
>
> This one also doesn't really solve the offset problem. In the
> version that I've proposed, there's no overhead if the offsets are
> equal, and it works if the offsets differ.

The linux/list.h implementation is a bit weird at first (at least I
found it so) but is perfectly adequate and very neat once you get used
to it. Here is your example program rewritten to use linux/list.h
(modulo insertion/traversal order - easily changeable):

------------------------ <list.c> ------------------------
#include <stdio.h>
#include "list.h"
struct node {
int data;
struct list_head list;
};
int main(void)
{
struct list_head queue, *tmp;
struct node n1, n2, n3;
n1.data = 1;
n2.data = 2;
n3.data = 3;
INIT_LIST_HEAD(&queue);
list_add(&n1.list, &queue);
list_add(&n2.list, &queue);
list_add(&n3.list, &queue);
tmp = queue.next;
while(tmp != &queue) {
struct node *n = list_entry(tmp, struct node, list);
printf("%d\n", n->data);
tmp = tmp->next;
}
list_del(&n2.list);
list_del(&n1.list);
list_del(&n3.list);
}
------------------------ </list.c> -----------------------
Regards,
Borislav

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:56    [from the cache]
©2003-2014 Jasper Spaans. Advertise on this site