/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>

struct node
{
   char *data;
   struct node *prev;
   struct node *next;
};

void add(struct node **first, char *data)
{
    struct node *extra,*last;
    extra = (struct node *)malloc(sizeof(struct node));
    extra->data = data;
    extra->prev = NULL;
    extra->next = NULL;
    if(*first==NULL)
    {
        *first = extra;
    }
    else
    {
        last = *first;
        while(last->next!=NULL) last=last->next;
        last->next = extra;
        extra->prev = last;
    }
}

void print(struct node *first)
{
    struct node *curr;
    curr = first;
    while(curr!=NULL)
    {
        printf("this=%p prev=%p next=%p data=%s\n",curr,curr->prev,curr->next,curr->data);
        curr = curr->next;
    }
}

void xadd(struct node **first, int ix, char *data)
{
    add(&first[ix], data);
}

void xprint(struct node **first,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("list %d:\n",i);
        print(first[i]);
    }
}

int main()
{
    int i;
    struct node **first = (struct node **)malloc(6*sizeof(struct node *));
    for(i=0;i<6;i++) first[i]=NULL;
    xadd(first, 0, "a");
    xadd(first, 0, "bb");
    xadd(first, 0, "ccc");
    xadd(first, 2, "xxx");
    xadd(first, 2, "yy");
    xadd(first, 2, "z");
    xadd(first, 4, "1");
    xadd(first, 4, "2");
    xadd(first, 4, "3");
    xprint(first,6);
    return 0;
}
