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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
#ifndef LINKLIST_LINKLIST_H #define LINKLIST_LINKLIST_H #include <cstdlib> #include "ListNode.h"
template <typename T> class List { private:
int _size ; ListNode<T>* header ; ListNode<T>* trailer ;
protected:
void init() ; int clear() ; void swap(T& x,T& y){T temp = x ; x = y ; y = temp ; } void copyNodes(ListNode <T>* p , int n) ; void merge(ListNode <T>* & , int , List<T>& , ListNode<T>* , int) ; void mergeSort(ListNode <T>* & p , int n) ; void insertionSort(ListNode <T>*,int); void selectionSort(ListNode <T>*,int);
public:
List(){ init();} ;
~List() ;
Rank size() const {return _size;} ;
bool empty() const {return _size<=0;} ;
T& operator[](int r) const ;
ListNode<T>* first() const {return header->succ ;} ;
ListNode<T>* last() const {return trailer->pred ;} ;
bool valid(ListNode<T>* p) {return p && (trailer != p) && (header != p) ;} ;
int disordered() const ;
ListNode<T>* find(T const& e) const {return find(e,_size,trailer); }
ListNode<T>* find(T const& e,int n,ListNode<T>* p) const ;
ListNode<T>* search(T const& e) const {return search(e,_size,trailer); } ;
ListNode<T>* search(T const& e,int n,ListNode<T>* p) const ;
ListNode<T>* selectMax(ListNode<T>* p,int n) ;
ListNode<T>* selectMax(){return selectMax(header->succ,_size) ;} ;
ListNode<T>* insertAsFirst(T const& e) ;
ListNode<T>* insertAsLast(T const& e) ;
ListNode<T>* insertAsBefore(ListNode<T>*,T const& e) ;
ListNode<T>* insertAsAfter(ListNode<T>*,T const& e) ;
T remove(ListNode<T>* p);
void merge(List<T>& L){merge(first(),_size,L,L.first(),L._size);}
void sort(ListNode<T>* p,int n);
void sort(){sort(first(),_size);};
int deduplicate();
int uniquify();
void reverse();
void traverse(void (*) (T&));
};
template <typename T> void List<T>::init() { header = new ListNode<T>; trailer = new ListNode<T>; header->succ = trailer; header->pred = NULL; trailer->pred = header; trailer->succ = NULL; _size = 0;
}
template <typename T> int List<T>::clear() { int oldSize = _size ; while(0 < _size) remove(header->succ); return oldSize ; }
template<typename T> void List<T>::copyNodes(ListNode <T>* p, int n){ init(); while(n--){ insertAsLast(p->data) ; p = p->succ ; } }
template <typename T> void List<T>::insertionSort(ListNode<T> * p, int n) { for(int r = 0 ; r < n ; r++){ insertAsAfter(search(p->data,r,p),p->data) ; p = p->succ ; remove(p->pred); } }
template <typename T> ListNode<T>* List<T>::selectMax(ListNode<T>* p,int n) { ListNode<T>* max = p ; for(ListNode<T>* cur = p ; 1 < n ; n--){ if((cur = cur->succ)->data >= max->data) max = cur ; } return max ; }
template <typename T> void List<T>::selectionSort(ListNode<T>* p, int n) { ListNode<T>* head = p->pred ; ListNode<T>* tail = p ; for(int i = 0 ; i < n ; i++) tail = tail->succ ; while(1 < n) { ListNode<T>* max = selectMax(head->succ,n) ; insertAsBefore(tail,remove(max)); tail = tail->pred ; n-- ; } }
template <typename T> void List<T>::merge(ListNode<T> *& p, int n, List<T> & L, ListNode<T> * q, int m) { ListNode<T>* pp = p->pred ; while(0 < m) if((0 < n) && (p->data <= q->data)){ if(q == (p = p->succ)) break ; n-- ; } else{ insertAsBefore(p,L.remove((q = q->succ)->pred)) ; m-- ; } p = pp->succ ;
}
template <typename T> void List<T>::mergeSort(ListNode<T> *&p, int n) { if(n < 2) return ; int m = n >> 1; ListNode<T>* q = p ; for(int i = 0 ; i < m ; i++) q = q->succ ; mergeSort(p,m) ; mergeSort(q,n-m ); merge(p,m,*this,q,n-m); }
template <typename T> List<T>::~List(){ clear() ; delete header ; delete trailer ; }
template <typename T> ListNode<T>* List<T>::find(T const& e,int n,ListNode<T>* p) const { while(0 < n--) if(e == (p = p->pred)->data) return p; return NULL; }
template <typename T> ListNode<T>* List<T>::search(T const& e,int n,ListNode<T>* p) const { while(0 <= n--) if(((p = p->pred)->data) <= e) break; return p;
}
template <typename T> ListNode<T>* List<T>::insertAsFirst(T const& e){ _size++ ; return header->insertAsSucc(e); } template <typename T> ListNode<T>* List<T>::insertAsLast(T const& e){ _size++ ; return trailer->insertAsPred(e); } template <typename T> ListNode<T>* List<T>::insertAsBefore(ListNode<T>* p , T const& e){ _size++ ; return p->insertAsPred(e); } template <typename T> ListNode<T>* List<T>::insertAsAfter(ListNode<T>* p , T const& e){ _size++ ; return p->insertAsSucc(e); }
template <typename T> T List<T>::remove(ListNode<T>* p){ T e = p->data; p->pred->succ = p->succ; p->succ->pred = p->pred; delete p ; _size-- ; return e ; }
template <typename T> int List<T>::deduplicate(){ if(_size < 2) return 0; int oldSize = _size ; ListNode<T>* p = header ; Rank r = 0 ; while(trailer != (p = p->succ)){ ListNode<T>* q = find(p->data,r,p); q ? remove(q) : r++ ; } return oldSize - _size ; }
template <typename T> int List<T>::uniquify(){ if(_size < 2) return 0; int oldSize = _size; ListNode<T>* p ; ListNode<T>* q ; for(p = header,q = p->succ ; trailer != q ; p = q , q = q->succ) if(p->data == q->data){remove(q) ; q = p ;} return oldSize - _size ; }
template <typename T> void List<T>::traverse(void (*visit) (T&)){ for(ListNode<T>* p = header->succ ; p != trailer ; p = p->succ) visit(p->data); }
template <typename T> void List<T>::sort(ListNode<T>* p,int n) { switch (random() % 3) { case 1: insertionSort(p,n) ; break; case 2: selectionSort(p,n) ; break; default: mergeSort(p,n) ; break;
} }
template <typename T> T& List<T>::operator[] (int r) const { ListNode<T>* p = first() ; while(0 < r--) p = p->succ ; return p->data ; }
template<typename T> int List<T>::disordered() const { int count = 0 ; for(ListNode<T>* p = header->succ ; p->succ != trailer ; p = p->succ){ if(p->data > p->succ->data) count++; } return count ; }
template<typename T> void List<T>::reverse() { ListNode<T>* begin = header; ListNode<T>* end = trailer; while( ( begin != end ) && ( end->succ != begin ) ){ swap(begin->data,end->data); begin = begin->succ; end = end->pred; } }
#endif
|