/*Program to illustrate insertion into a linked list Adapted from Mullick & Sarner, Structure of Programming Languages, 1996 pp. 93-94...nested structs eliminated Language: C (gcc target) */ #include #include #include #define NO_APPTS 4 typedef struct appointment {char name[21]; /*holds name of appointee, up to 20 chars*/ int hr; /*hour of appointment*/ int min; /*minute of appointment*/ struct appointment *next; /*link to next record*/ }APPT; APPT* insertit(APPT *first, char person[], int hrtime, int mintime); int main(void) { APPT *head, /*pointer to head of linked list*/ *p; /*pointer to traverse list*/ char client[21]; /*name of appointee*/ int apphour, /*hour of appointment*/ appmin, /*minute of appointment*/ j, /*counter*/ k; /*counter - appointment number*/ char ch, /*holder*/ nl; /*place to keep newline character*/ head=NULL; for(j=0;j<21;j++) /*blank out cient array*/ client[j]=' '; for(k=0;kname); printf("The time of the appointmetnt is: %d:%d\n",p->hr, p->min); p=p->next; } printf("Hit any character to continue"); scanf("%c",&ch); } APPT* insertit (APPT *first, char person[], int hrtime, int mintime) /* function to maintain linked structure Adapted from Mullick & Sarner, Structure of Programming Languages, 1996, pp. 93-94 */ { APPT *p, *q, *newp; int found, len, i; found=0; q=first; p=first; /*find the appropriate position for the new entry*/ while ((p!=NULL) && (!found)) { if ((p->hr < hrtime) || ((p->hr == hrtime) && (p->min <= mintime))) {q=p; p=p->next; } else found=1; } newp=(APPT*) malloc(sizeof(APPT)); /*create new record pointed to by newp*/ newp->hr=hrtime; newp->min=mintime; strncpy(newp->name, person, 21); /*adjust pointers*/ newp->next=p; if(q!=p) q->next=newp; else /*insertion is at the beginning*/ first=newp; return(first); }