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.
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
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.
#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;
int link;
}stockrecord;
stockrecord records[MAX];
typedef in listpointer;
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;
}