home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 14view / vf_list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.1 KB  |  110 lines

  1. /*
  2.  *    vf_list -- linked list management functions
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include "vf.h"
  8.  
  9. /*
  10.  *    vf_mklst -- create a new list by allocating a node,
  11.  *    making it point to itself, and setting its values
  12.  *    to zero (appropriately cast)
  13.  */
  14.  
  15. DNODE *
  16. vf_mklst()
  17. {
  18.     DNODE *new;
  19.  
  20.     new = (DNODE *)malloc(sizeof (DNODE));
  21.     if (new != NULL) {
  22.         new->d_next = new;
  23.         new->d_prev = new;
  24.         new->d_lnum = new->d_flags = 0;
  25.         new->d_line = (char *)NULL;
  26.     }
  27.     return (new);
  28. } /* end vf_mklst() */
  29.  
  30.  
  31. /*
  32.  *    vf_alloc -- create a pool of available nodes
  33.  */
  34.  
  35. DNODE *
  36. vf_alloc(n)
  37. int n;
  38. {
  39.     register DNODE *new;
  40.     register DNODE *tmp;
  41.  
  42.     /* allocate a block of n nodes */
  43.     new = (DNODE *)malloc(n * sizeof (DNODE));
  44.  
  45.     /* if allocation OK, string the nodes in one direction */
  46.     if (new != NULL) {
  47.         for (tmp = new; 1 + tmp - new < n; tmp = tmp->d_next)
  48.             tmp->d_next = tmp + 1;
  49.         tmp->d_next = (DNODE *)NULL;
  50.     }
  51.  
  52.     return (new);    /* pointer to free list */
  53. } /* end vf_alloc() */
  54.  
  55.  
  56. /*
  57.  *    vf_ins -- insert a node into a list after the specified node
  58.  */
  59.  
  60. DNODE *
  61. vf_ins(node, avail)
  62. DNODE *node, *avail;
  63. {
  64.     DNODE *tmp;
  65.     DNODE *vf_alloc(int);
  66.  
  67.     /*
  68.      *  check freelist -- get another block of nodes
  69.      *  if the list is almost empty
  70.      */
  71.     if (avail->d_next == NULL)
  72.         if ((avail->d_next = vf_alloc(N_NODES)) == NULL)
  73.             /* not enough memory */
  74.             return (DNODE *)NULL;
  75.  
  76.     /* get a node from the freelist */
  77.     tmp = avail;
  78.     avail = avail->d_next;
  79.  
  80.     /* insert the node into the list after node */
  81.     tmp->d_prev = node;
  82.     tmp->d_next = node->d_next;
  83.     node->d_next->d_prev = tmp;
  84.     node->d_next = tmp;
  85.  
  86.     /* point to next node in the freelist */
  87.     return (avail);
  88. } /* end vf_ins() */
  89.  
  90.  
  91. /*
  92.  *    vf_del -- delete a node from a list
  93.  */
  94.  
  95. DNODE *
  96. vf_del(node, avail)
  97. DNODE *node, *avail;
  98. {
  99.     /* unlink the node from the list */
  100.     node->d_prev->d_next = node->d_next;
  101.     node->d_next->d_prev = node->d_prev;
  102.  
  103.     /* return the deleted node to the freelist */
  104.     node->d_next = avail;
  105.     avail = node;
  106.  
  107.     /* point to the new freelist node */
  108.     return (avail);
  109. } /* end vf_del() */
  110.