home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / sep91.zip / 9N09021A < prev    next >
Text File  |  1991-07-21  |  3KB  |  97 lines

  1.  
  2. /* --------------------------------------------------------------
  3.  
  4. FUNCTION ADD_NODE: The steps to add a new node to the list in
  5. ascending alphabetic order are:
  6.  
  7. A.  Get a string from the user.
  8.  
  9. B.  If this string already exists in the list increment its occurrence
  10. count and get out.
  11.  
  12. C.  Have a unique string so get a free node from free node list.  If no
  13. more, complain and get out.
  14.  
  15. D.  Get space for new node's string.  If no more, complain and get out.
  16.  
  17. E.  Initialize new node's string pointer and count members, and copy 
  18. user string to allocated space. Also increment active node count.
  19.  
  20. F.  If this is the first node, make its fwd and bwd pointers go 
  21. nowhere and make both root and tail pointers point to it.
  22.  
  23. G.  Else if its string is less than that of the first node, insert it 
  24. at the beginning of the list by initializing the newly inserted node,
  25. the bwd pointer of the old first node, and adjusting the root pointer.
  26.  
  27. H.  Else the new node is to be inserted after an existing link.
  28.  
  29. I.  And if the new node is inserted after the last node in the list, 
  30.     adjust the tail pointer accordingly.
  31.  
  32. -------------------------------------------------------------- */
  33.  
  34. void add_node(void)
  35. {
  36.     Node *pnew_node;    /* ptr to new node */
  37.     Node *ploc_node;    /* ptr to located node */
  38.     char *pstring;        /* ptr to alloced string space */
  39.     char string[21];    /* tmp holder for node's string */
  40.  
  41. /*A*/    printf("\n   Enter new string: ");
  42.     scanf("%20s", string);
  43.  
  44. /*B*/    ploc_node = locate_node(string, EXACT);    
  45.     if (ploc_node != NULL) {
  46.         ++ploc_node->count;
  47.         printf("\n   Duplicate added");
  48.         return;
  49.     }
  50.  
  51. /*C*/    pnew_node = get_free_node();
  52.     if (pnew_node == NULL) {
  53.         printf("\n   No nodes available\n");
  54.         return;
  55.     }
  56.  
  57. /*D*/    pstring = malloc(strlen(string) + 1);
  58.     if (pstring == NULL) {
  59.         printf("\n   No string space available\n");
  60.         return;
  61.     }
  62.  
  63. /*E*/    ++nodes_in_use;
  64.     pnew_node->pstring = pstring;
  65.     pnew_node->count = 1;
  66.     strcpy(pnew_node->pstring, string);
  67.  
  68. /*F*/    if (proot_node == NULL) {
  69.         pnew_node->pfwd = NULL;
  70.         pnew_node->pbwd = NULL;
  71.         proot_node = pnew_node;
  72.         ptail_node = pnew_node;
  73.     }
  74.     else {
  75. /*G*/        ploc_node = locate_node(string, INEXACT);    
  76.         if (ploc_node == NULL) {
  77.             pnew_node->pfwd = proot_node;
  78.             pnew_node->pbwd = NULL;
  79.             proot_node->pbwd = pnew_node;
  80.             proot_node = pnew_node;
  81.         }
  82. /*H*/        else {
  83.             pnew_node->pfwd = ploc_node->pfwd;
  84.             pnew_node->pbwd = ploc_node;
  85.             ploc_node->pfwd = pnew_node;
  86.             if (pnew_node->pfwd != NULL) {
  87.                 pnew_node->pfwd->pbwd = pnew_node;
  88.             }
  89. /*I*/            else {
  90.                 ptail_node = pnew_node;
  91.             }
  92.         }
  93.     }
  94.         printf("\n   Node added");
  95. }
  96.  
  97.