3.6.2 Lists Stored in an Array of Records

Instead of being stored in dynamic memory, list records may be stored in an array of records. The declarations required are illustrated for the example of stocks.

#define MAXSIZE 5

#define MAX 100

#define NULL -1

typedef struct

{

int month;

int day;

int year;

}date;

typedef struct

{

char name[MAXSIZE];

int shares;

float value;

date datebought;

}infofield;

typedef struct listrecord

{

infofield info;

>Comment

int link;

}stockrecord;

stockrecord records[MAX];

>Comment

typedef in listpointer;

For this implementation the null pointer value is -1. Here, variables of type listpointer contain integer array indices. The implementation for some of the functions operating on the list are

next(pointer)

/* Returns a copy of the

link field in the record

pointed to by pointer

*/

listpointer pointer;

{

return(records[pointer].link);

}

setnull()

/* Returns a null pointer */

{

return(-1);

}

setlink(pointerl,pointer2)

/* Sets the link field of the

record pointed to by pointer1

to the contents of pointer2

*/

listpointer pointer1,pointer2;

{

records[pointer1].link = pointer2;

}

This implementation assumes that records is declared nonlocally to these functions, so that they may reference it. With this implementation, the records array plays the role of the dynamic memory. It is necessary to write an avail function that must keep track of those slots in records that are not in use, and hence can be allocated when storage for a list record is requested.