Define a linked list of a linked list

Roger House roger at everexn.com
Fri Sep 21 05:57:21 AEST 1990


In <13821 at hydra.gatech.EDU> ce1zzes at prism.gatech.EDU (Eric Sheppard) writes:


>The problem: I'm not quite sure how to handle a linked list of a linked list.
>Structure :  First member 4 char header, successive members 6 char data and
>               1 char flag.
>aaaa
>bbbbbb c
>bbbbbb c
> ...
>bbbbbb c     Number of successive members must be variable; best way to
>             implement this is with a linked list.

>I will be building a file of structures of the above format, and need to
>manipulate and organize them, preferably in memory; a kind of database. 
>Depending on how efficiently the structures can be organized, I shouldn't have
>to worry about running out of memory (small-scale database running on 
>mainframe).  Since the total number of linked lists is not fixed, how can a
>linked list of the above linked list be defined?

>One idea:  Place the header and next-structure-pointer into the outer list, 
>and the successive members into the inner list.

>struct inner { 
>  char data[7]; 
>  char flag; 
>  struct inner *next; 
>  };
>typedef struct inner INNR;
>typedef INNR *PT_INNR;

>struct outer { 
>  char header[5]; 
>  struct INNR;               <- not sure about this declaration. Use pointer?
>  struct outer *next; 
>  };
>typedef struct outer OUTR;
>typedef OUTR *PT_OUTR;


Your solution looks okay except for the member you marked as "not sure
about this declaration".  You do want a pointer here.  Here's how I would
do it, using different notation than "inner" and "outer":

	typedef struct head
	{
		char	header[5];
	 struct head	*nexthead;
	 struct item	*nextitem;

	} HEAD;


	typedef struct item
	{
		char	data[7];
		char	flag;
	 struct item	*nextitem;

	} ITEM;


Each individual list consists of a HEAD and zero or more ITEMs.  The HEAD
of a list points at the first ITEM on the list, and each ITEM points at 
the next ITEM.  In addition, the entire collection of lists is linked by 
the nexthead member of HEAD.

Hope this is of some use.

						Roger House



More information about the Comp.lang.c mailing list