Főoldal | Névtérlista | Osztályhierarchia | Betűrendes lista | Adatszerkezetek | Könyvtárak | Fájllista | Névtértagok | Adatmezők | Globális elemek

DoubleLinkedList.h

Ugrás a fájl dokumentációjához.
00001 /*
00002     Double Linked List
00003     Copyright (C) 2006  giszo
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 #ifndef DOUBLELINKEDLIST_H
00020 #define DOUBLELINKEDLIST_H
00021 #include <stdlib.h>
00022 #include <Locking.h>
00023 
00024 template<typename T>
00025 class DoubleLinkedList {
00026   public:
00027     DoubleLinkedList() {
00028       firstItem = NULL;
00029     }
00030 
00031     ~DoubleLinkedList() {
00032       clear();
00033     }
00034 
00035     bool add( T data ) {
00036       Lock l( cs );
00037 
00038       struct _ListItem* newItem = ( struct _ListItem* )malloc( sizeof( struct _ListItem ) );
00039       if ( newItem == NULL ) { return false; }
00040 
00041       newItem->data = data;
00042       if ( firstItem == NULL ) {
00043         firstItem = newItem;
00044         newItem->prev = NULL;
00045         newItem->next = NULL;
00046       } else {
00047         newItem->prev = NULL;
00048         newItem->next = firstItem;
00049         firstItem->prev = newItem;
00050         firstItem = newItem;
00051       }
00052 
00053       return true;
00054     }
00055 
00056     T get( int ind ) {
00057       Lock l( cs );
00058 
00059       int _ind = 0;
00060       struct _ListItem* tmpItem = firstItem;
00061 
00062       while ( tmpItem ) {
00063         if ( _ind == ind ) {
00064           return tmpItem->data;
00065         }
00066 
00067         _ind++;
00068         tmpItem = tmpItem->next;
00069       }
00070 
00071       return NULL;
00072     }
00073 
00074     void remove( int ind ) {
00075       Lock l( cs );
00076 
00077       struct _ListItem* delItem = NULL;
00078 
00079       if ( firstItem == NULL ) { return; }
00080       if ( ind == 0 ) {
00081         delItem = firstItem;
00082         firstItem = firstItem->next;
00083         if ( firstItem ) { firstItem->prev = NULL; }
00084         free( delItem );
00085       } else {
00086         int _ind = 0;
00087         struct _ListItem* tmpItem = firstItem;
00088 
00089         while ( tmpItem ) {
00090           if ( _ind == ind ) {
00091             delItem = tmpItem;
00092             if ( tmpItem->prev ) { tmpItem->prev->next = tmpItem->next; }
00093             if ( tmpItem->next ) { tmpItem->next->prev = tmpItem->prev; }
00094             free( delItem );
00095             return;
00096           }
00097 
00098           _ind++;
00099           tmpItem = tmpItem->next;
00100         }
00101       }
00102     }
00103 
00104     void remove( T data ) {
00105       Lock l( cs );
00106 
00107       struct _ListItem* delItem = NULL;
00108 
00109       if ( firstItem == NULL ) { return; }
00110       if ( firstItem->data == data ) {
00111         delItem = firstItem;
00112         firstItem = firstItem->next;
00113         if ( firstItem ) { firstItem->prev = NULL; }
00114         free( delItem );
00115       } else {
00116         struct _ListItem* tmpItem = firstItem;
00117 
00118         while ( tmpItem ) {
00119           if ( tmpItem->data == data ) {
00120             delItem = tmpItem;
00121             if ( tmpItem->prev ) { tmpItem->prev->next = tmpItem->next; }
00122             if ( tmpItem->next ) { tmpItem->next->prev = tmpItem->prev; }
00123             free( delItem );
00124             return;
00125           }
00126 
00127           tmpItem = tmpItem->next;
00128         }
00129       }
00130     }
00131 
00132     void clear() {
00133       Lock l( cs );
00134 
00135       struct _ListItem* delItem = NULL;
00136       struct _ListItem* tmpItem = NULL;
00137 
00138       tmpItem = firstItem;
00139       while ( tmpItem ) {
00140         delItem = tmpItem;
00141         tmpItem = tmpItem->next;
00142 
00143         free( delItem );
00144       }
00145     }
00146 
00147     int size() {
00148       Lock l( cs );
00149 
00150       int size = 0;
00151       struct _ListItem* tmpItem = firstItem;
00152 
00153       while ( tmpItem ) {
00154         size++;
00155         tmpItem = tmpItem->next;
00156       }
00157 
00158       return size;
00159     }
00160 
00161     int indexOf( T data ) {
00162       Lock l( cs );
00163 
00164       int ind = 0;
00165       struct _ListItem* tmpItem = firstItem;
00166 
00167       while ( tmpItem ) {
00168         if ( tmpItem->data == data ) {
00169           return ind;
00170         }
00171 
00172         ind++;
00173         tmpItem = tmpItem->next;
00174       }
00175 
00176       return -1;
00177     }
00178 
00179   private:
00180     struct _ListItem {
00181       struct _ListItem* prev;
00182       struct _ListItem* next;
00183 
00184       T data;
00185     };
00186 
00187     CriticalSection cs;
00188     struct _ListItem* firstItem;
00189 };
00190 
00191 #endif

SourceForge.netLogo