00001 #ifndef TABLEAUDYN_H 00002 #define TABLEAUDYN_H 00003 00004 #include <iostream> 00005 #include <stdio.h> 00006 #include <stdlib.h> 00007 #include <cstdlib> 00008 00009 using namespace std; 00010 00014 template<class T> class TableauDyn{ 00015 public: 00016 //Constructeurs 00021 TableauDyn(); 00022 00028 TableauDyn(int nb); 00029 00030 00036 TableauDyn(const TableauDyn<T> &l); 00037 //Destructeurs 00038 ~TableauDyn(); 00039 //Ajouts 00040 00047 TableauDyn<T> & Cons(T val, int pos); 00048 00054 TableauDyn<T> & ConsTete(T val); 00055 00061 TableauDyn<T> & ConsQueue(T val); 00062 00063 //Modification 00069 void Modifier(int pos, T elt); 00070 00076 void SetTaille(unsigned int nb); 00077 //Destructions 00078 00084 TableauDyn<T> & Detruit(unsigned int i); 00085 00092 TableauDyn<T> & DetruitVirtuel(unsigned int pos); 00093 //Consultations 00094 00099 unsigned int Card() const; 00100 00105 T Tete() const; 00106 00112 T Valeur(int i) const; 00113 00119 T operator[](int i) const; 00120 00124 void Afficher() const; 00125 //Affectations 00131 const TableauDyn<T> &operator=(const TableauDyn<T> &l); 00132 protected: 00136 T *mTab; 00137 00141 int mCard; 00142 }; 00143 00144 //Constructeurs 00145 00146 template<class T> TableauDyn<T>::TableauDyn(){ 00147 mCard=0; 00148 mTab=NULL; 00149 } 00150 00151 template<class T> TableauDyn<T>::TableauDyn(int nb){ 00152 mCard=nb; 00153 mTab=new T [nb]; 00154 } 00155 00156 template<class T> TableauDyn<T>::TableauDyn(const TableauDyn<T> &l){ 00157 delete [] mTab; 00158 mCard=l.Card(); 00159 mTab=new T [mCard]; 00160 for(unsigned int i=0; i<l.Card(); i++){ 00161 mTab[i]=l[i]; 00162 } 00163 } 00164 //Destructeurs 00165 00166 template<class T> TableauDyn<T>::~TableauDyn(){ 00167 delete [] mTab; 00168 mCard=0; 00169 } 00170 //Ajouts 00171 00172 template<class T> TableauDyn<T> & TableauDyn<T>::Cons(T val, int pos){ 00173 //Allocation du tableau temporaire 00174 T *tab=new T[mCard+1]; 00175 //Recopie jusqu pos 00176 for(int i=0; i<pos; i++){ 00177 tab[i]=mTab[i]; 00178 } 00179 //Construction en pos de la valeur 00180 tab[pos]=val; 00181 //Recopie du reste 00182 for(int i=pos; i<mCard; i++){ 00183 tab[i+1]=mTab[i]; 00184 } 00185 //Desctruction de l'ancien tableau 00186 delete[] mTab; 00187 //Nouvelle allocation 00188 mTab=new T[mCard+1]; 00189 //Recopie 00190 for(int i=0; i<mCard+1; i++){ 00191 mTab[i]=tab[i]; 00192 } 00193 mCard++; 00194 //Destruction du tableau temporaire 00195 delete[] tab; 00196 return (*this); 00197 } 00198 00199 template<class T> TableauDyn<T> & TableauDyn<T>::ConsTete(T val){ 00200 Cons(val,0); 00201 return (*this); 00202 } 00203 00204 template<class T> TableauDyn<T> & TableauDyn<T>::ConsQueue(T val){ 00205 Cons(val,mCard); 00206 return (*this); 00207 } 00208 //Modification 00209 template<class T> void TableauDyn<T>::Modifier(int pos, T elt){ 00210 mTab[pos]=elt; 00211 } 00212 00213 template<class T> void TableauDyn<T>::SetTaille(unsigned int nb){ 00214 delete[] mTab; 00215 mCard=nb; 00216 mTab=new T [nb]; 00217 } 00218 00219 //Destructions 00220 template<class T> TableauDyn<T> & TableauDyn<T>::DetruitVirtuel(unsigned int pos){ 00221 mCard--; 00222 return (*this); 00223 } 00224 00225 template<class T> TableauDyn<T> & TableauDyn<T>::Detruit(unsigned int pos){ 00226 T *tab=new T[mCard-1]; 00227 //on recopie tout sauf pos 00228 int k=0; 00229 for(int i=0; i<mCard; i++){ 00230 if(i!=pos){ 00231 tab[k]=mTab[i]; 00232 k++; 00233 } 00234 } 00235 //on supprime mTab 00236 delete[] mTab; 00237 mTab=new T[mCard-1]; 00238 for(int i=0; i<mCard-1; i++){ 00239 mTab[i]=tab[i]; 00240 } 00241 //on supprime tab 00242 delete[] tab; 00243 if(mCard>0) mCard--; 00244 return (*this); 00245 } 00246 //Consultations 00247 template<class T> unsigned int TableauDyn<T>::Card() const{ 00248 return mCard; 00249 } 00250 template<class T> T TableauDyn<T>::Tete() const{ 00251 return mTab[0]; 00252 } 00253 template<class T> T TableauDyn<T>::Valeur(int i) const{ 00254 return mTab[i]; 00255 } 00256 template<class T> T TableauDyn<T>::operator[](int i) const{ 00257 return mTab[i]; 00258 } 00259 template<class T> void TableauDyn<T>::Afficher() const{ 00260 for(int i=0; i<mCard; i++){ 00261 cout<<mTab[i]<<" "; 00262 } 00263 } 00264 //Affectations 00265 template<class T> const TableauDyn<T> &TableauDyn<T>::operator=(const TableauDyn<T> &l){ 00266 delete [] mTab; 00267 mCard=l.Card(); 00268 mTab=new T [mCard]; 00269 for(unsigned int i=0; i<l.Card(); i++){ 00270 mTab[i]=l[i]; 00271 } 00272 return (*this); 00273 } 00274 00275 #endif