home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 308_01 / sgl_list.hpp < prev    next >
C/C++ Source or Header  |  1990-09-20  |  3KB  |  103 lines

  1.  
  2. /*
  3.     TITLE:          C++ SingleList Object Header;
  4.     DESCRIPTION:    "Header file for singly-linked list object";
  5.     VERSION:        1.0;
  6.     DATE:           9/21/90;
  7.     COMPILERS:      Borland Turbo C++ V.1.0;
  8.     KEYWORDS:       list object header;
  9.     FILENAME:       Sgl_List.hpp;
  10.     SEE-ALSO:       Sgl_List.cpp;
  11.  
  12.     AUTHOR:         Michael Kelly
  13.             254 Gold St. Boston, Ma. 02127
  14.             Copyright 1990;
  15.  
  16.     COPYRIGHT:    This code may not be commercially distributed
  17.             without prior arrangement with the author.  It
  18.             may be used by programmers, without royalty, for
  19.             their personal programs and for "one of a kind"
  20.             or "custom" applications, provided that said
  21.             programmers assume all liability concerning
  22.             same.
  23.  
  24.     NOTES:        These singly-linked lists have the same
  25.             functionality as the doubly-linked versions
  26.             ( DoubleList and CircularDoubleList )
  27.             but sacrifice speed to save space.  A
  28.             link pointer is saved for each item in
  29.             the list.
  30. */
  31.  
  32.  //             ----------< SingleList Object Header >----------
  33.  
  34.  
  35. #if !defined(SGL_LIST_HPP)
  36. #define SGL_LIST_HPP
  37.  
  38. #include <string.h>
  39. #include "baselist.hpp"
  40.  
  41. class  SingleList  :  public BaseList  {
  42.   protected:
  43.     struct SingleLink  {
  44.     SingleLink    *Next;
  45.     Entry           *entry;
  46.     };
  47.  
  48.     SingleLink    *First, *Last, *Current;
  49.     int compare(void *item1, void *item2)
  50.     { return strcmp((const char *) item1, (const char *) item2); }
  51.  
  52.   public:
  53.     SingleList(void)
  54.     { First = Last = Current = NULL; }    // constructor
  55.  
  56.     ~SingleList(void);                    // destructor
  57.  
  58.     const void *get_ptr(void)
  59.     { return (Current != NULL) ?
  60.         (const void *)Current->entry->item : 
  61.         lerror = EMPTY_LIST, (const void *)NULL; }
  62.  
  63.     size_t get_size(void)
  64.     { return (Current != NULL) ?
  65.         Current->entry->itemsize : lerror = EMPTY_LIST, 0; }
  66.  
  67.     Boolean add_item(void *item, size_t itemsize, Place place = LastPlace);
  68.     Boolean delete_item(void);
  69.     Boolean get_item(void *itembuf);
  70.     Boolean remove_item(void *itembuf)
  71.     {
  72.       if(! get_item(itembuf))
  73.         return False;
  74.  
  75.       return delete_item();
  76.     }
  77.  
  78.     int compare_item(void *item1);
  79.     Boolean find_item(void *item1);
  80.     Boolean replace_item(void *newitem, size_t newsize);
  81.  
  82.     Boolean first(void)
  83.     { return (Current = First) ? True : lerror = EMPTY_LIST, False; }
  84.     Boolean last(void)
  85.     { return (Current = Last) ? True : lerror = EMPTY_LIST, False; }
  86.     Boolean next(void)
  87.     { return (Current != Last) ?
  88.         Current = Current->Next, True : False; }
  89.     Boolean prev(void);        // more involved than next() with 1 link
  90.     Boolean sort(void);
  91. };  // class SingleList
  92.  
  93.  
  94. class CircularSingleList  :  public SingleList  {
  95.   public:
  96.     Boolean next(void)
  97.     {  return (SingleList::next()) ? True : first(); }
  98.     Boolean prev(void)
  99.     {  return (SingleList::prev()) ? True : last();  }
  100. };
  101.  
  102. #endif
  103.