home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_10 / 9n10036a < prev    next >
Text File  |  1991-08-15  |  2KB  |  76 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "c_calls.h"
  6.  
  7. void insert_cell(LIST *list, const char *name)
  8.  
  9. /*
  10. Insert a new cell in the list with the specified name and return a
  11. pointer to the head of the list.  The new cell is inserted in
  12. alphabetical order.
  13. */
  14.  
  15. {
  16. LIST new_cell;
  17.  
  18. if ((*list == NULL) || (namecmp(name, (*list)->name) < 0))
  19.      {
  20.      if ((new_cell = malloc(sizeof(CELL))) == NULL)
  21.           error("out of memory");
  22.  
  23.      new_cell->calls = new_cell->called_from = NULL;
  24.      strcpy(new_cell->name, name);
  25.  
  26.      if (*list == NULL)
  27.           new_cell->next = NULL;
  28.      else
  29.           new_cell->next = *list;
  30.  
  31.      *list = new_cell;
  32.      }
  33. else
  34.      if (stricmp(name, (*list)->name) != 0)
  35.           insert_cell((&(*list)->next), name);
  36. }
  37.  
  38.  
  39. void delete_cell(LIST *list, const char *name)
  40.  
  41. /* Delete the cell with the specified name from the list. */
  42.  
  43. {
  44. LIST ptr;
  45.  
  46. /* If cell to be deleted is at head of list... */
  47. if ((*list != NULL) && (stricmp(name, (*list)->name) == 0))
  48.      {
  49.      /* Delete the cell. */
  50.      ptr = *list;
  51.      *list = (*list)->next;
  52.      free(ptr);
  53.      }
  54.  
  55. else
  56.      /* Try to delete the cell from the rest of the list. */
  57.      if (*list != NULL)
  58.           delete_cell(&(*list)->next, name);
  59. }
  60.  
  61.  
  62. LIST find_cell(LIST list, const char *name)
  63.  
  64. /*
  65. Return a pointer to the cell containing the specified name
  66. if it is found in the list.  Otherwise return NULL.
  67. */
  68.  
  69. {
  70. for (; list != NULL; list = list->next)
  71.      if (stricmp(name, list->name) == 0)
  72.           return list;
  73.  
  74. return NULL;
  75. }
  76.