#define MAXSIZE 5
#define NULL 0
#define SENTINEL -1
typedef struct
{
int month;
int day;
int year;
}date;
typedef struct
{
char name[MAXSIZE];
int share;
float value;
date datebought;
}infofield;
typedef struct listrecord
{
infofield info;
struct listrecord *link;
}stockrecord,*listpointer;
listpointer next(pointer)
/* Returns a copy of the
link field in the record
pointed to by pointer
*/
listpointer pointer;
{
return(pointer->link);
}
listpointer setnull()
/* Returns a null pointer */
{
return(NULL);
}
setlink(pointer1,pointer2)
/* Sets the link field of the
record pointed to by pointer1
to the contents of pointer2
*/
listpointer pointer1,pointer2;
{
pointer1->link = pointer2;
}
setinfo(pointer1,pointer2)
/* Sets the infofield of the record
pointed to by pointer1 to the
infofield of the record
pointed to by pointer2
*/
listpointer pointer1,pointer2;
{
int i;
for(i=0;i<MAXSIZE;i++)
pointer1->info.name[i] = pointer2->
info.name[i];
pointer1->info.shares = pointer2->
info.shares;
pointer1->info.value = pointer2->info.value;
pointer1->info.datebought.month = pointer2->
info.datebought.month;
pointer1->info.datebought.day = pointer2->
info.datebought.day;
pointer1->info.datebought.year = pointer2->
info.datebought.year;
}
listpointer avail()
/* Returns a pointer to storage
allocated for a record of type
stockrecord
*/
{
listpointer malloc();
return(malloc(sizeof(stockrecord)));
}
anotherrecord(recordpointer)
/* Returns true if there is
another record
*/
listpointer recordpointer;
{
listpointer setnull();
return(recordpointer != setnull());
}
listpointer getnextrecord()
/* Inputs the data for the new
record and returns a pointer
to a record containing the data
in its infofield, or, if there
are no new records,returns
a null pointer
*/
{
listpoiner setnull();
static stockrecord nextrecord;
printf("Enter number of shares\n");
scanf("%d",&(nextrecord.info.shares));
if(nextrecord.info.shares != SENTINEL)
{
printf(" Enter the stock name - less than
%d characters\n",MAXSIZE);
scanf("%s",nextrecord.info.name);
printf(" Enter the price of one share of
the stock\n");
scanf("%f",&(nextrecord.info.value));
printf(" Enter the month day year of the
stock purchase\n");
scanf("%d %d %d",&(nextrecord.info.
datebought.month),
&(nextrecord.info.datebought.day),
&(nextrecord.info.datebought.year));
return(&nextrecord);
}
else
return(setnull());
}
printrecord(recordpointer)
/* Prints the contents of the infofield
of the record pointed to by recordpointer
*/
listpointer recordpointer;
{
printf(" The stock is %s\n",
recordpointer->info.name);
printf(" The number of shares is %d\n",
recordpointer->info.shares);
printf(" The value of a share is %f\n",
recordpointer->info.value);
printf(" The date of purchase is %d %d %d\n\n",
recordpointer->info.datebought.month,
recordpointer->info.datebought.day,
recordpointer->info.datebought.year);
}