home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / r_la4_01 / agent2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-22  |  2.3 KB  |  77 lines

  1. /* AGENT2.C - From page 309 of "Microsoft C Programming for the */
  2. /* IBM by Robert Lafore. This is the final program of the data- */
  3. /* base started on page 292. it uses linked lists.              */
  4. /****************************************************************/
  5.  
  6. #include <stdio.h>           /*defines 'stdin'*/
  7. #define TRUE 1
  8. struct prs {
  9.    char name[30];
  10.    int agnumb;
  11.    float height;
  12.    struct prs *ptrnext;       /*ptr to next structure*/
  13. };
  14. struct prs *ptrfirst, *ptrthis, *ptrnew;
  15.  
  16. main()
  17. {
  18. char ch;
  19.  
  20.    ptrfirst = (struct prs *)NULL;         /*no input yet*/
  21.    while (TRUE) {
  22.       printf("\nType 'e' to enter new agent");
  23.       printf("\n  'L' to list all agents: ");
  24.       ch = getche();
  25.       switch (ch) {
  26.          case 'e':
  27.             newname();
  28.             break;
  29.          case 'L':
  30.             listall();
  31.             break;
  32.          default:
  33.             puts("\nEnter only selections listed");
  34.       }
  35.    }
  36. }
  37.  
  38. /* newname() */      /*puts a new agent in the database*/
  39. newname()
  40. {
  41.    ptrnew = (struct prs *) malloc(sizeof(struct prs));
  42.    if (ptrfirst == (struct prs *)NULL)  /*if none already, save addr*/
  43.       ptrfirst = ptrthis = ptrnew;
  44.    else {                     /*not first item, go to end of list*/
  45.       ptrthis = ptrfirst;     /* (start at begin ) */
  46.       while (ptrthis->ptrnext != (struct prs *)NULL)
  47.          ptrthis = ptrthis->ptrnext;      /*find next item*/
  48.       ptrthis->ptrnext = ptrnew;          /*pt to new item*/
  49.       ptrthis = ptrnew;                   /*go to new item*/
  50.    }
  51.    printf("\nEnter name: ");
  52.    gets(ptrthis->name);
  53.    printf("Enter number: ");
  54.    scanf("%d", &ptrthis->agnumb);
  55.    printf("Enter height: ");
  56.    scanf("%f", &ptrthis->height);
  57.    fflush(stdin);
  58.    ptrthis->ptrnext = (struct prs *)NULL;    /*this is end*/
  59. }
  60.  
  61. /* listall() */      /*lists all agents and data */
  62. listall()
  63. {
  64.    if (ptrfirst == (struct prs *)NULL) {  /*if empty list*/
  65.       printf("\nEmpty list.\n");
  66.       return;
  67.    }
  68.    ptrthis = ptrfirst;           /*start at first item*/
  69.    do {
  70.       printf("\nName: %s\n", ptrthis->name);
  71.       printf("Number: %03d\n", ptrthis->agnumb);
  72.       printf("Height: %4.2f\n", ptrthis->height);
  73.       ptrthis = ptrthis->ptrnext;      /*move to next item*/
  74.    } while(ptrthis != (struct prs *)NULL);   /*quit on null ptr*/
  75. }
  76.  
  77.