home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / sllist.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-14  |  7.9 KB  |  280 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: sllist.h
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 12/29/1996  
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. This is a generic singly linked list class dervied from the
  32. SNodeBase class and the SLListBase class.
  33.  
  34. Changes 
  35. ================================================================
  36. 08/19/1998 - Remove duplicate definition of overloaded
  37. assignment operator.
  38. Changed by: Doug Gaer
  39. ================================================================
  40. */
  41. // ----------------------------------------------------------- // 
  42. #ifndef __SLLIST_HPP
  43. #define __SLLIST_HPP
  44.  
  45. #include "sllistb.h"
  46.  
  47. // (S)ingly Linked List (N)ode class 
  48. template<class TYPE>
  49. class SNode: public SNodeBase
  50. {
  51. public:
  52.   SNode() { } // Implicitly call default constructor for Data
  53.   SNode(const TYPE &X) : Data(X) { } // Call copy constructor
  54.  
  55. public:
  56.   SNode<TYPE> *GetNext() { return (SNode<TYPE> *)Next; }
  57.   const SNode<TYPE> *GetNext() const { return (SNode<TYPE> *)Next; }
  58.  
  59. public:
  60.   TYPE Data;
  61. };
  62.  
  63. // (S)ingly (L)inked (L)ist class declaration
  64. template<class TYPE>
  65. class SLList : public SLListBase
  66. {
  67. public:
  68.   SLList() { }
  69.   virtual ~SLList();
  70.   SLList(const SLList<TYPE> &X);
  71.   void operator=(const SLList<TYPE> &X);
  72.  
  73. public:
  74.   int Copy(const SLList<TYPE> &List);
  75.   int Cat(const SLList<TYPE> &X) { return SLListBase::Cat(X); }
  76.   const SNode<TYPE> *Find(const TYPE &X, const SNode<TYPE> *ptr=0) const;
  77.   SNode<TYPE> *Find(const TYPE &X, SNode<TYPE> *ptr=0);
  78.   int DeleteNext(SNode<TYPE> *Node, TYPE *X = 0);
  79.   int DeleteFront(TYPE *X = 0);
  80.   SNode<TYPE> *StoreNode(const TYPE &X);
  81.   SNode<TYPE> *AddToFront(const TYPE &X);
  82.   SNode<TYPE> *AddToBack(const TYPE &X);
  83.   SNode<TYPE> *AddAfter(const TYPE &X, SNode<TYPE> *Node);
  84.   SNode<TYPE> *GetHeader() { return(SNode<TYPE> *) this; }
  85.   const SNode<TYPE> *GetHeader() const { return(SNode<TYPE> *) this; }
  86.   SNode<TYPE> *GetFront() { return(SNode<TYPE> *)SLListBase::GetFront(); }
  87.   SNode<TYPE> *GetBack() { return (SNode<TYPE> *)SLListBase::GetBack(); }
  88.   
  89.   const SNode<TYPE> *GetFront() const { // Read only version
  90.     return(SNode<TYPE> *)SLListBase::GetFront();
  91.   }
  92.  
  93.   const SNode<TYPE> *GetBack() const { // Read only version
  94.     return (SNode<TYPE> *)SLListBase::GetBack();
  95.   }
  96.  
  97.   int IsHeader(const SNode<TYPE> *Node) const {
  98.     return SLListBase::IsHeader(Node);
  99.   }
  100.   TYPE *GetFrontNode() {
  101.     SNode<TYPE> *ptr = (SNode<TYPE> *)SLListBase::GetFront();
  102.     return IsHeader(ptr) ? 0 : &(ptr->Data);
  103.   }
  104.  
  105.   const TYPE *GetFrontNode() const {
  106.     SNode<TYPE> *ptr = (SNode<TYPE> *)SLListBase::GetFront();
  107.     return IsHeader(ptr) ? 0 : &(ptr->Data);
  108.   }
  109.  
  110.   TYPE *GetBackNode() {
  111.     SNode<TYPE> *ptr = (SNode<TYPE> *)SLListBase::GetBack();
  112.     return IsHeader(ptr) ? 0 : &(ptr->Data);
  113.   }
  114.  
  115.   const TYPE *GetBackNode() const {
  116.     SNode<TYPE> *ptr = (SNode<TYPE> *)SLListBase::GetBack();
  117.     return IsHeader(ptr) ? 0 : &(ptr->Data);
  118.   }
  119.  
  120. public:
  121.   void InsertAfter(SNode<TYPE> *A, SNode<TYPE> *B) {
  122.     SLListBase::InsertAfter(A, B);
  123.   }
  124.  
  125.   void AttachToFront(SNode<TYPE> *Node) {
  126.     SLListBase::AttachToFront(Node);
  127.   }
  128.  
  129.   void AttachToBack(SNode<TYPE> *Node) {
  130.     SLListBase::AttachToBack(Node);
  131.   }
  132.  
  133.   SNode<TYPE> *RmvFront() {
  134.     return(SNode<TYPE> *)(SLListBase::RmvFront());
  135.   }
  136.  
  137.   SNode<TYPE> *RmvNext(SNode<TYPE> *Node) {
  138.     return(SNode<TYPE> *)(SLListBase::RmvNext(Node));
  139.   }
  140.  
  141. protected:
  142.   virtual SNode<TYPE> *AllocNode(const TYPE &X);
  143.   virtual SNodeBase *DupNode(const SNodeBase *Node);
  144.   virtual void FreeNode(SNodeBase *Node);
  145.   
  146. public: // Overloaded operators
  147.   int operator+=(const SLList<TYPE> &X) { return Cat(X); }
  148. };
  149.  
  150. template<class TYPE>
  151. SLList<TYPE>::~SLList()
  152. {
  153.   Clear();
  154. }
  155.  
  156. template<class TYPE>
  157. SLList<TYPE>::SLList(const SLList<TYPE> &X)
  158. {
  159.   Copy(X);
  160. }
  161.  
  162. template<class TYPE>
  163. void SLList<TYPE>::operator=(const SLList<TYPE> &X)
  164. {
  165.   Copy(X);
  166. }
  167.  
  168. template<class TYPE>
  169. int SLList<TYPE>::Copy(const SLList<TYPE> &List)
  170. {
  171.   return SLListBase::Copy(List);
  172. }
  173.  
  174. template<class TYPE>
  175. SNode<TYPE> *SLList<TYPE>::AllocNode(const TYPE &X)
  176. {
  177.   return new SNode<TYPE>(X); 
  178. }
  179.  
  180. template<class TYPE>
  181. SNodeBase *SLList<TYPE>::DupNode(const SNodeBase *Node)
  182. {
  183.   return AllocNode(((SNode<TYPE> *)Node)->Data);
  184. }
  185.  
  186. template<class TYPE>
  187. void SLList<TYPE>::FreeNode(SNodeBase *Node)
  188. {
  189.   delete((SNode<TYPE> *)Node);
  190. }
  191.  
  192. template<class TYPE>
  193. const SNode<TYPE> *SLList<TYPE>::
  194. Find(const TYPE &X, const SNode<TYPE> *ptr) const
  195. // Returns the first node having an element that matched X
  196. {
  197.   if(ptr == 0) ptr = GetFront();
  198.  
  199.   while(!IsHeader(ptr)) { // Scan until end of list
  200.     if(ptr->Data == X) return ptr; // Match found
  201.     ptr = ptr->GetNext();
  202.   }
  203.   return 0; // No match
  204. }
  205.  
  206. template<class TYPE>
  207. SNode<TYPE> *SLList<TYPE>::Find(const TYPE &X, SNode<TYPE> *ptr)
  208. // Returns the first node having an element that matched X
  209. {
  210.   if(ptr == 0) ptr = GetFront();
  211.  
  212.   while(!IsHeader(ptr)) { // Scan until end of list
  213.     if(ptr->Data == X) return ptr; // Match found
  214.     ptr = ptr->GetNext();
  215.   }
  216.   return 0; // No match
  217. }
  218.  
  219. template<class TYPE>
  220. int SLList<TYPE>::DeleteNext(SNode<TYPE> *Node, TYPE *X)
  221. {
  222.   SNode<TYPE> *ptr = RmvNext(Node);
  223.   if(ptr) {
  224.     if(X) *X = ptr->Data; // Copy Data into X if X != 0
  225.     FreeNode(ptr);
  226.     return 1; // Return 1 if successful
  227.   }
  228.   return 0; 
  229. }
  230.  
  231. template<class TYPE>
  232. int SLList<TYPE>::DeleteFront(TYPE *X)
  233. {
  234.   SNode<TYPE> *ptr = RmvFront();
  235.   if(ptr) {
  236.     if(X) *X = ptr->Data; // Copy Data into X if X != 0
  237.     FreeNode(ptr);
  238.     return 1; // Return 1 if successful
  239.   }
  240.   return 0; 
  241. }
  242.  
  243. template<class TYPE>
  244. SNode<TYPE> *SLList<TYPE>::StoreNode(const TYPE &X)
  245. {
  246.   SNode<TYPE> *ptr = AllocNode(X);
  247.   if(ptr) AttachToBack(ptr);
  248.   return ptr; // Return a pointer to the node added
  249. }
  250.  
  251. template<class TYPE>
  252. SNode<TYPE> *SLList<TYPE>::AddToFront(const TYPE &X)
  253. {
  254.   SNode<TYPE> *ptr = AllocNode(X);
  255.   if(ptr) AttachToFront(ptr);
  256.   return ptr; // Return a pointer to the node added
  257. }
  258.  
  259. template<class TYPE>
  260. SNode<TYPE> *SLList<TYPE>::AddToBack(const TYPE &X)
  261. {
  262.   SNode<TYPE> *ptr = AllocNode(X);
  263.   if(ptr) AttachToBack(ptr);
  264.   return ptr; // Return a pointer to the node added
  265. }
  266.  
  267. template<class TYPE>
  268. SNode<TYPE> *SLList<TYPE>::AddAfter(const TYPE &X, SNode<TYPE> *Node)
  269. {
  270.   SNode<TYPE> *ptr = AllocNode(X);
  271.   if(ptr) InsertAfter(Node, ptr);
  272.   return ptr; // Return a pointer to the node added
  273. }
  274.  
  275. #endif // __SLLIST_HPP
  276. // ----------------------------------------------------------- //
  277. // ------------------------------- //
  278. // --------- End of File --------- //
  279. // ------------------------------- //
  280.