home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter15 / l15-4.c < prev    next >
Text File  |  1997-06-18  |  944b  |  27 lines

  1. /*
  2. Finds the first node in a linked list with a value field greater
  3. than or equal to a key value, and returns a pointer to the node
  4. preceding that node (to facilitate insertion and deletion), or a
  5. NULL pointer if no such value was found. Assumes the list is
  6. terminated with a tail node pointing to itself as the next node. */
  7.  
  8. #include <stdio.h>
  9. #include "llist.h"
  10.  
  11. struct LinkNode *FindNodeBeforeValueNotLess(
  12.    struct LinkNode *HeadOfListNode, int SearchValue)
  13. {
  14.    struct LinkNode *NodePtr = HeadOfListNode;
  15.  
  16.    while ( (NodePtr->NextNode->NextNode != NodePtr->NextNode) &&
  17.          (NodePtr->NextNode->Value < SearchValue) )
  18.       NodePtr = NodePtr->NextNode;
  19.  
  20.    if (NodePtr->NextNode->NextNode == NodePtr->NextNode)
  21.       return(NULL);     /* we found the sentinel; failed search */
  22.    else
  23.       return(NodePtr);  /* success; return pointer to node preceding
  24.                            node that was >= */
  25. }
  26.  
  27.