1 /* 2 * This code is released under the GNU General Public License. See COPYING for 3 * details. Copyright 2003 John Spray: spray_john@users.sourceforge.net 4 */ 5 6 7 #ifndef LLIST_H 8 #define LLIST_H 9 10 #include <stdlib.h> 11 12 template<class TYPE> 13 class LListItem{ 14 public: 15 LListItem<TYPE>* next; 16 TYPE data; 17 }; 18 19 20 template<class TYPE> 21 class LList{ 22 public: LList()23 LList() 24 { 25 head=NULL; 26 count=0; 27 } ~LList()28 ~LList() 29 { 30 Flush(); 31 } Add(TYPE * target)32 TYPE* Add(TYPE* target) 33 { 34 LListItem<TYPE>* newitem; 35 36 newitem=new LListItem<TYPE>; 37 newitem->data=*target; 38 newitem->next=head; 39 head=newitem; 40 count++; 41 return &newitem->data; 42 } 43 Del(LListItem<TYPE> * target)44 LListItem<TYPE>* Del(LListItem<TYPE>* target) 45 { 46 LListItem<TYPE>* curr=NULL; 47 LListItem<TYPE>* targetnext=NULL; 48 curr=head; 49 while(curr) 50 { 51 if(curr->next==target){ 52 curr->next=target->next; 53 break; 54 } 55 curr=curr->next; 56 } 57 58 if(target==head) 59 head=target->next; 60 61 targetnext=target->next; 62 delete target; 63 count--; 64 return targetnext; 65 66 67 } Flush()68 void Flush() 69 { 70 LListItem<TYPE>* curr=NULL; 71 curr=head; 72 while(curr) 73 { 74 curr=Del(curr); 75 } 76 count=0; 77 } 78 LListItem<TYPE>* head; 79 int count; 80 }; 81 82 #endif //LLIST_H 83 84 85 86 87