[comp.lang.c] Define a linked list of a linked list

ce1zzes@prism.gatech.EDU (Eric Sheppard) (09/19/90)

I've just given myself a crash course in C, to bring myself up to speed. My 
first project, a FORTRAN to C program conversion, was a resounding success.
The resulting program uses linked structures for dynamic data input; no more
hard-coded arrays!  Makes the new program much more efficient and flexible.
My next project is to convert another one of my old FORTRAN programs, but this
one seems a bit more difficult.

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;

Are there any dangers involved with this setup?  Advice would be appreciated...

Eric, tinkerer-at-large

rmj@tcom.stc.co.uk (The Phantom Countertenor) (09/20/90)

In article <13821@hydra.gatech.EDU> ce1zzes@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.
}
}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;
}
}Are there any dangers involved with this setup?  Advice would be appreciated...

This looks fine to me. I would be inclined to use a pointer in the
struct outer declaration, but the only pressing reason for doing so is
if you think you may get a situation of having a header with no data
attached (using a NULL pointer as meaning no more data hanging off here,
as usual.
-- 
* Windsinger                 * "But soft, what light through yonder
* rmj@islay.tcom.stc.co.uk   *      airlock breaks?"
* rmj@tcom.stc.co.uk         *    --RETURN TO THE FORBIDDEN PLANET
* rmj10@phx.cam.ac.uk        *  You've gotta be cruel to be Khund!

roger@everexn.com (Roger House) (09/21/90)

In <13821@hydra.gatech.EDU> ce1zzes@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