This example_dlist page shows how to "derive" and use a doubly-linked list using the librnr dlistvoid.h header file and the library functions DListVoid<x> defined in dlistvoid.c.
Example header file:
#include <stdio.h>
#define DLIST_DNAME Zoo
#define DLIST_DTYPE Zoo_T
typedef struct
{
int m_nZid;
const char *m_sSciName;
const char *m_sComName;
char *m_sPetName;
{
}
{
delete(pData);
}
{
if( pData != NULL )
{
fprintf(fp, "%03d \"%s\": %s (%s)",
}
}
Example main():
#include <stdio.h>
#include <string.h>
{
{"elephas maximus", "Asiatic elephant"},
{"loxodonta africana", "African bush elephant"},
{"python sebae", "African rock python"},
{"alligator mississippiensis", "American alligator"},
{"ramphastos toco", "Toco toucan"},
{"lasiorhinus latifrons", "southern hairy-nosed wombat"},
{"rhinoceros sondaicus", "Javan rhinoceros"},
};
{
{"Shoes", "alligator mississippiensis"},
{"Fluffy", "loxodonta africana"},
{"Snuggles", "python sebae"},
{"Wild Bill", "ramphastos toco"},
{"The Prez", "lasiorhinus latifrons"},
};
{
const char **sCat;
sCat = AnimalCatalog[0];
for(sCat=AnimalCatalog[0]; sCat[0]!=
NULL; sCat++)
{
if( !strcmp(sCat[0], sSciName) )
{
return pZooAni;
}
}
return pZooAni;
}
int main(
int argc,
char *argv[])
{
DListZoo_T *pMyZoo;
DListZooIter_T zooIter;
int i;
printf("I bought an empty zoo.\n");
for(i=0; FlatEarthZooDeal[i][0]!=
NULL; ++i)
{
pMyAni =
NewZooAnimal(FlatEarthZooDeal[i][1], FlatEarthZooDeal[i][0]);
DListZooAppend(pMyZoo, pMyAni);
}
printf("\nI bought some animals from the zoo in Flat Earth, NE.\n");
DListZooDeleteNode(pMyZoo, DListZooFindNode(pMyZoo, &zooMatch));
printf("\nAlas, Fluffy died.\n");
DListZooPrepend(pMyZoo, pMyAni);
printf("\nBut I got a new primo 'phant from the kid down the block.\n");
printf("\nTime to give my menagerie their yearly vitamin shots.\n");
for(pMyAni=DListZooIterDataFirst(pMyZoo, &zooIter);
pMyAni=DListZooIterDataNext(&zooIter))
{
}
pMyAni =
NewZooAnimal(
"lasiorhinus latifrons",
"wittlewomby1");
DListZooInsert(pMyZoo, 4, pMyAni);
pMyAni =
NewZooAnimal(
"lasiorhinus latifrons",
"wittlewomby2");
DListZooInsert(pMyZoo, 4, pMyAni);
printf("\nI guess \"The Prez\" wasn't male. "
"Pass out the bubble gum cigars!\n");
DListZooDeleteAllNodes(pMyZoo);
printf("\nI decided to let my animals go free - in Alaska.\n");
DListZooDelete(pMyZoo);
printf("\nGoodbye.\n");
return 0;
}
Generated output:
$ ../loc/bin.x86_64/example_dlist >example_dlist.run
I bought an empty zoo.
I bought some animals from the zoo in Flat Earth, NE.
Node 0: 000 "Shoes": alligator mississippiensis (American alligator)
Node 1: 001 "Fluffy": loxodonta africana (African bush elephant)
Node 2: 002 "Snuggles": python sebae (African rock python)
Node 3: 003 "Wild Bill": ramphastos toco (Toco toucan)
Node 4: 004 "The Prez": lasiorhinus latifrons (southern hairy-nosed wombat)
Alas, Fluffy died.
Node 0: 000 "Shoes": alligator mississippiensis (American alligator)
Node 1: 002 "Snuggles": python sebae (African rock python)
Node 2: 003 "Wild Bill": ramphastos toco (Toco toucan)
Node 3: 004 "The Prez": lasiorhinus latifrons (southern hairy-nosed wombat)
But I got a new primo 'phant from the kid down the block.
Node 0: 005 "eMouse": elephas maximus (Asiatic elephant)
Node 1: 000 "Shoes": alligator mississippiensis (American alligator)
Node 2: 002 "Snuggles": python sebae (African rock python)
Node 3: 003 "Wild Bill": ramphastos toco (Toco toucan)
Node 4: 004 "The Prez": lasiorhinus latifrons (southern hairy-nosed wombat)
Time to give my menagerie their yearly vitamin shots.
injected eMouse
injected Shoes
injected Snuggles
injected Wild Bill
injected The Prez
I guess "The Prez" wasn't male. Pass out the bubble gum cigars!
Node 0: 005 "eMouse": elephas maximus (Asiatic elephant)
Node 1: 000 "Shoes": alligator mississippiensis (American alligator)
Node 2: 002 "Snuggles": python sebae (African rock python)
Node 3: 003 "Wild Bill": ramphastos toco (Toco toucan)
Node 4: 007 "wittlewomby2": lasiorhinus latifrons (southern hairy-nosed wombat)
Node 5: 006 "wittlewomby1": lasiorhinus latifrons (southern hairy-nosed wombat)
Node 6: 004 "The Prez": lasiorhinus latifrons (southern hairy-nosed wombat)
I decided to let my animals go free - in Alaska.
Goodbye.