All pastes #454512 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-04-23 17:22 UTC
rendered paste body
/*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 <stdio.h>
#include <malloc.h>
#include <string.h>
#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;k<NO_APPTS;k++)    /*appointments get made in this loop*/
   {printf("Enter name of person for whom the appointment\n");
    printf("is to be made...max 20 characters  :");
    gets(client);
    printf("Enter the hour of the desired appointment:  ");
    scanf("%d",&apphour);
    printf("Enter the minute of the desired appointment:  ");
    scanf("%d", &appmin);
    scanf("%c", &nl);
    head=insertit (head,client,apphour,appmin);
 /*blank out the client array again*/
    for (j=0;j<21;j++)
      client[j]=' ';
   }                        /*end of for loop; when we get here all
                              appointments have been made so print the list*/

   p=head;                  /*point p to the beginning of the list*/
   while (p!=NULL)
     {printf("The name of the person is: %s  ", p->name);
      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);
}