home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / c / WLIB.ZIP / WLINK.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-24  |  5.2 KB  |  137 lines

  1. #ifndef WLinkIncluded
  2. #define WLinkIncluded
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5. // 1916 Brooks #205, Missoula, MT  59801
  6. //
  7. // voice phone:  (406)543-7543
  8. // modem phone:  (406)543-1144 (2400N81)
  9. //  CompuServe:  72707,207
  10. //    Internet:  72707.207@CompuServe.com
  11.  
  12. #include <stddef.h>
  13. #include <WMisc.h>
  14.  
  15. #ifdef MAJORBBS
  16.   #include <stdlib.h>
  17. #endif
  18.  
  19. struct LinkedListElement
  20.   {
  21.     friend class LinkedList;
  22.     LinkedListElement *PrevRec, *NextRec;
  23.     void* Rec;
  24.   };
  25.  
  26. class LinkedList
  27.   {
  28.       LinkedListElement *RootRec, *CurRec;
  29.       Long NumRecs;
  30.       Long CurRecNum; // 0...NumRecs-1
  31.     public:
  32.       LinkedList();
  33.         // an empty form
  34.       ~LinkedList();
  35.       void* Root();
  36.         // returns rec 0.  CurRec is set to RootRec.
  37.       void* Cur() {return CurRec->Rec;}
  38.       Long Size() {return NumRecs;}
  39.         // the number of things in list
  40.       Long Top() {return(NumRecs-1);}
  41.       Long Num() {return CurRecNum;}
  42.         // 0..Top():  Cur's index
  43.       void* Next();
  44.         // Cur is moved up and returned
  45.       void* Prev();
  46.         // Cur is moved back and returned
  47.       void* Last();
  48.         // like Root only it's the last element
  49.       Bool Insert(void* NewRec);
  50.         // will be inserted between Prev and Cur
  51.       Bool Add(void* NewRec);
  52.         // will be inserted between Cur and Next
  53.       Bool Append(void* NewRec);
  54.         // will be the last record in the list
  55.       Bool Push(void* NewRec){return Append(NewRec);}
  56.       void DelCur();
  57.         // link is destroyed.  Cur will be old Next.  Object is not deleted
  58.       #ifndef MAJORBBS
  59.       void DelCurObj()  // same as DelCur cept object is destroyed too
  60.         // Note!  Only the object memory is freed.  No object destructors are called
  61.         {delete Cur(); DelCur();}
  62.       #endif
  63.       void* Pop();
  64.         // Returns pointer to last rec. Link is destroyed. Cur will be old Prev.
  65.       void* operator[](Long Index);
  66.  
  67.     //  to do:  add in stuff like Add(void* NewRec,...);
  68.  
  69.       #ifdef MAJORBBS
  70.         void* operator new(size_t size){return malloc(size);}
  71.         void  operator delete(void* p) {free(p);}
  72.       #endif
  73.   };
  74.  
  75. #ifdef MAJORBBS
  76.  
  77.   #define CreateLinkedListClass(ClassName,ObjType)                        \
  78.                                                                           \
  79.   class ClassName: public LinkedList                                      \
  80.     {                                                                     \
  81.       public:                                                             \
  82.         ObjType& Root(){return *(ObjType*)LinkedList::Root();}            \
  83.         ObjType& Last(){return *(ObjType*)LinkedList::Last();}            \
  84.         ObjType& Cur() {return *(ObjType*)LinkedList::Cur();}             \
  85.         ObjType& Next(){return *(ObjType*)LinkedList::Next();}            \
  86.         ObjType& Prev(){return *(ObjType*)LinkedList::Prev();}            \
  87.         ObjType& Pop() {return *(ObjType*)LinkedList::Pop();}             \
  88.         Bool Insert(ObjType& NewRec){return LinkedList::Insert(&NewRec);} \
  89.         Bool Add(ObjType& NewRec)   {return LinkedList::Add(&NewRec);}    \
  90.         Bool Append(ObjType& NewRec){return LinkedList::Append(&NewRec);} \
  91.         Bool Push(ObjType& NewRec)  {return LinkedList::Push(&NewRec);}   \
  92.         ObjType& operator[](Long Index)                                   \
  93.           {return *(ObjType*)(LinkedList::operator[](Index));}            \
  94.         void DelAllObj(); /* {while (Size()) DelCurObj();} */             \
  95.     };
  96.  
  97. #else
  98.  
  99.   #define CreateLinkedListClass(ClassName,ObjType)                        \
  100.                                                                           \
  101.   class ClassName: public LinkedList                                      \
  102.     {                                                                     \
  103.       public:                                                             \
  104.         ObjType& Root(){return *(ObjType*)LinkedList::Root();}            \
  105.         ObjType& Last(){return *(ObjType*)LinkedList::Last();}            \
  106.         ObjType& Cur() {return *(ObjType*)LinkedList::Cur();}             \
  107.         ObjType& Next(){return *(ObjType*)LinkedList::Next();}            \
  108.         ObjType& Prev(){return *(ObjType*)LinkedList::Prev();}            \
  109.         ObjType& Pop() {return *(ObjType*)LinkedList::Pop();}             \
  110.         Bool Insert(ObjType& NewRec){return LinkedList::Insert(&NewRec);} \
  111.         Bool Add(ObjType& NewRec)   {return LinkedList::Add(&NewRec);}    \
  112.         Bool Append(ObjType& NewRec){return LinkedList::Append(&NewRec);} \
  113.         Bool Push(ObjType& NewRec)  {return LinkedList::Push(&NewRec);}   \
  114.         ObjType& operator[](Long Index)                                   \
  115.           {return *(ObjType*)(LinkedList::operator[](Index));}            \
  116.         void DelCurObj()                                                  \
  117.           {delete ((ObjType*)LinkedList::Cur()); LinkedList::DelCur();}   \
  118.         void DelAllObj(); /* {while (Size()) DelCurObj();} */             \
  119.     };
  120.  
  121. #endif
  122.  
  123. class Queue:public LinkedList
  124.   {
  125.     public:
  126.       Queue(){}
  127.       void* Pop();
  128.   };
  129.  
  130. class Stack:public LinkedList
  131.   {
  132.     public:
  133.       Stack(){}
  134.   };
  135.  
  136. #endif
  137.