1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| #include "lstruc_t.h"
using namespace std;
template<class T> MySortedList<T>::MySortedList()
{
pList=NULL;
nbElem=0;
mode=CROISSANT;
}
template<class T> MySortedList<T>::~MySortedList()
{
noeud<T> *pCur, *pPrec;
pCur=pList;
while(pCur!=NULL)
{
pPrec=pCur;
pCur=pCur->ptSv;
delete pPrec;
}
}
template<class T>void MySortedList<T>::Affiche(void)const
{
cout << "Affichage de la liste triee." << endl;
if(pList!=NULL)
{
noeud<T> *pCur=pList;
while(pCur!=NULL)
{
cout << endl;
pCur->val.Affiche();
cout << endl;
pCur=pCur->ptSv;
}
cout << "Fin de la liste." << endl;
}
else
cout << "Liste vide." << endl;
}
template<class T>void MySortedList<T>::insere(T data)
{
noeud<T> *pCur, *pPrecedent;
noeud<T> *pNew=new noeud<T>;
pNew->val=data;
pNew->ptSv=NULL;
if(pList==NULL) //Insertion au début quand liste vide
{
//cout << "Liste vide" << endl;
pList=pNew;
}
else
{
if(pNew->val<=pList->val) //Insertion au début
{
//cout << "Liste début" << endl;
pNew->ptSv=pList;
pList=pNew;
}
else
{
pCur=pList;
while(pCur!=NULL && data>pCur->val)
{
pPrecedent=pCur;
pCur=pCur->ptSv;
}
if(pCur==NULL && data>pPrecedent->val) //Insertion en fin de liste
{
//cout << "Liste fin" << endl;
pPrecedent->ptSv=pNew;
}
else //Insertion au milieu de la liste
{
//cout << "Liste milieu" << endl;
pPrecedent->ptSv=pNew;
pNew->ptSv=pCur;
}
}
}
nbElem++;
}
template<class T>T MySortedList<T>::retirePremier(void)
{
//if(nbElem<=0)
//{
// cout << "La liste triee est vide." << endl;
// return -1;
//}
noeud<T> *pLast=pList;
T val=pList->val;
pList=pList->ptSv;
delete pLast;
nbElem--;
return val;
}
template<class T>T MySortedList<T>::retireDernier(void)
{
T val;
//if(nbElem<=1)
//{
// val=retirePremier();
// return val;
//}
noeud<T> *pLast=pList;
noeud<T> *pEnd;
while(pLast->ptSv!=NULL)
{
pEnd=pLast;
pLast=pLast->ptSv;
}
pEnd->ptSv=NULL;
val=pLast->val;
delete pLast;
nbElem--;
return val;
}
template<class T>int MySortedList<T>::retirerElement(T _elem)
{
if(pList!=NULL)
{
noeud<T> *pCur=pList, *pPrecedent=pList;
if(pCur->ptSv==NULL)
{
if(_elem == pCur->val)
{
nbElem--;
pList=NULL;
//delete pCur;
return 1;
}
}
else
{
while(pCur->ptSv!=NULL)
{
pCur=pCur->ptSv;
if(_elem == pCur->val)
{
nbElem--;
pPrecedent->ptSv=pCur->ptSv;
//delete pCur;
return 1;
}
pPrecedent=pCur;
}
}
}
return 0;
}
template<class T>int MySortedList<T>::size(void)const
{
return nbElem;
}
template<class T>bool MySortedList<T>::vide(void)const
{
if(pList==NULL)
return true;
else
return false;
}
template<class T>int MySortedList<T>::save(fstream &f)
{
//Nombre d'element pour la lecture
f.write((char *)&nbElem, sizeof(int));
//Ecriture de la liste
IterateurSl<T> itSL(*this);
if(!MySortedList::vide())
{
do{ // Federation Admin
f.write((char*)&(&itSL), sizeof(T));
}while(++itSL);
}
return 1;
}
template<class T>int MySortedList<T>::load(fstream &f)
{
int nbElemTemp;
//Nombre d'element pour la lecture
f.read((char *)&nbElemTemp, sizeof(int));
//Ecriture de la liste
T temp;
for(int i=0;i<nbElemTemp;i++)
{
f.read((char *)&temp, sizeof(T));
insere(temp);
}
return 1;
}
template class MySortedList<ConcepteurAlbum>;
//template class MySortedList<Date>; |