home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap11 / rolo2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-10  |  2.8 KB  |  116 lines

  1. /* rolo2.c --  demonstrates a linked list */
  2.  
  3. #include <stdio.h>      /* for NULL  and stdin */
  4. #include <string.h>     /* for strdup()        */
  5. #include <malloc.h>     /* for malloc()        */
  6.  
  7. #define MAXN 79
  8.  
  9. struct cardstruct {          /* global pattern */
  10.     char first[MAXN],
  11.          last[MAXN],
  12.          middle[MAXN];
  13.     unsigned long street_no;
  14.     char street[MAXN],
  15.          city[MAXN],
  16.          state[MAXN];
  17.     unsigned long zip;
  18.     unsigned int area;
  19.     unsigned long phone;
  20.     struct   cardstruct *nextcard;
  21. };
  22.  
  23. main()
  24. {
  25.     int i;
  26.     struct cardstruct card, *first, *current;
  27.  
  28.     first = (struct cardstruct *)malloc(sizeof(struct cardstruct));
  29.     if(first == NULL)
  30.         exit(1);
  31.     if (Input(&card) != 0)
  32.         exit(1);
  33.     *first = card;
  34.     current = first;
  35.  
  36.     while (Input(&card) == 0)
  37.         {
  38.         current->nextcard =
  39.             (struct cardstruct *)malloc(sizeof(struct cardstruct));
  40.         if(current->nextcard == NULL)
  41.             exit(1);
  42.         current = current->nextcard;
  43.         *current = card;
  44.         }
  45.     current->nextcard = NULL;
  46.  
  47.     Dumplist(first);
  48. }
  49.  
  50. Dumplist(struct cardstruct *head)
  51. {
  52.     do
  53.         {
  54.         Showcard(head);
  55.         } while ((head = head->nextcard) != NULL);
  56. }
  57.  
  58. Showcard(struct cardstruct *cardptr)
  59. {
  60.     printf("\n\n");
  61.  
  62.     printf("%s %s %s\n", cardptr->first, cardptr->middle, 
  63.             cardptr->last);
  64.     printf("%ld %s, %s, %s %ld\n", cardptr->street_no, 
  65.             cardptr->street, cardptr->city, cardptr->state,
  66.             cardptr->zip);
  67.     printf("(%d) %ld\n", cardptr->area, cardptr->phone);
  68. }
  69.  
  70. Input(struct cardstruct *cardp)
  71. {
  72.     char *Str_Input();
  73.     long Lint_Input();
  74.  
  75.     printf("\n<new card> (Empty first name Quits)\n");
  76.     strcpy(cardp->first,Str_Input("First Name"));
  77.     if (*(cardp->first) == '\0')
  78.         return (1);
  79.     strcpy(cardp->last,Str_Input("Last Name"));
  80.     strcpy(cardp->middle,Str_Input("Middle Name"));
  81.     cardp->street_no = Lint_Input("Street Number");
  82.     strcpy(cardp->street,Str_Input("Street"));
  83.     strcpy(cardp->city,Str_Input("City"));
  84.     strcpy(cardp->state,Str_Input("State"));
  85.     cardp->zip = Lint_Input("Zip Code");
  86.     cardp->area = (int)Lint_Input("Area Code");
  87.     cardp->phone = Lint_Input("Phone Number");
  88.     return (0);
  89. }
  90.  
  91. char *Str_Input(char *prompt)
  92. {
  93.     char buf[MAXN + 1], *ptr;
  94.  
  95.     printf("%s: ", prompt);
  96.     if (fgets(buf, MAXN, stdin) == NULL)
  97.         exit(0);
  98.     buf[strlen(buf) - 1] = '\0'; /* strip '\n' */
  99.     if ((ptr = strdup(buf)) == NULL)
  100.         exit(0);
  101.     return (ptr);
  102. }
  103.  
  104. long Lint_Input(char *prompt)
  105. {
  106.     char buf[MAXN + 1];
  107.     long  num;
  108.  
  109.     printf("%s: ", prompt);
  110.     if (fgets(buf, MAXN, stdin) == NULL)
  111.         exit(0);
  112.     if (sscanf(buf, "%ld", &num) != 1)
  113.         num = 0;
  114.     return (num);
  115. }
  116.