

amptest
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
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 |
#ifndef LIB_SCP_QLIST #define LIB_SCP_QLIST #ifndef NULL #define NULL 0 #endif template<class T> class QList { private: typedef struct node { T item; node *next,*prev; }node; int len; // Size of list node *start,*end; // Pointers to start and end public: QList(); // Class constructor ~QList(); // Class destructor void push_back(const T i); // Push item at the back of list void push_front(const T i);// Push item at the front of the list void pop_back(); // Pops item from back void pop_front(); // Pops item from front T front(); // get item from front T back(); // get item from back int size(); // Returns size of list void clear(); // Clears list void clear(unsigned int index); // Clears list T get(unsigned int index); // Get item at given index T& at(unsigned int index); // Get item at given index // Array operator T& operator[](unsigned int index); const T& operator[](unsigned int index) const; // Not realy needed // Non - critical functions int length(); int indexOf(T val); }; // Constructor template<class T> QList<T>::QList() { len = 0; start = NULL; end = NULL; } // Destructor template<class T> QList<T>::~QList() { clear(); } // Push at front template<class T> void QList<T>::push_front(const T i) { node *tmp = new node; tmp->item = i; tmp->next = NULL; tmp->prev = NULL; if(start==NULL) // If list is empty { start = tmp; end = tmp; } else // Insert at start { tmp->next = start; start->prev = tmp; start = tmp; } len++; // Increase size counter } // Push at back template<class T> void QList<T>::push_back(const T i) { node *tmp = new node; tmp->item = i; tmp->next = NULL; tmp->prev = NULL; if(end==NULL) // If list is empty { start = tmp; end = tmp; } else // Insert at the end { tmp->prev = end; end->next = tmp; end = tmp; } len++; // Increase size counter } // Pop from front template<class T> void QList<T>::pop_front() { if(start!=NULL) { node *tmp = start; start = start->next; if(start!=NULL) // Re-link next item to NULL start->prev = NULL; else // List became empty so we need to clear end end = NULL; delete tmp; len--; // Decrease counter } } // Pop from back template<class T> void QList<T>::pop_back() { if(end!=NULL) { node *tmp = end; end = end->prev; if(end!=NULL) //Re-link previous item to NULL end->next = NULL; else // List became empty so we need to clear start start = NULL; delete tmp; len--; // Decrease counter } } // Get item from front template<class T> T QList<T>::front() { if(start!=NULL) return start->item; //TODO: Catch error when list is empty } //Get item from back template<class T> T QList<T>::back() { if(end!=NULL) return end->item; //TODO: Catch error when list is empty } // Get size template<class T> int QList<T>::size() { return this->len; } // Clear list template<class T> void QList<T>::clear() { node *tmp = start; while(start!=NULL) { tmp = start; start = start->next; delete tmp; // Delete item len--; // Decrease counter } end = NULL; } template<class T> void QList<T>::clear(unsigned int index) { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) { if(i==index) { if(tmp->prev!=NULL) tmp->prev->next = tmp->next; else start = tmp->next; if(tmp->next!=NULL) tmp->next->prev = tmp->prev; else end = tmp->prev; len--; // Decrease counter delete tmp; // Delete item break; } else tmp=tmp->next; } } // Get at index template<class T> T QList<T>::get(unsigned int index) { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) { if(i==index) return tmp->item; else tmp=tmp->next; } //TODO: Catch error when index is out of range } template<class T> T& QList<T>::at(unsigned int index) { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) { if(i==index) return tmp->item; else tmp=tmp->next; } //TODO: Catch error when index is out of range } // Get length template<class T> int QList<T>::length() { return this->len; } // Get index of value template<class T> int QList<T>::indexOf(T val) { for(int i=0;i<this->size();i++) if(this->at(i) == val) return i; return -1; } // Array operators template<class T> T& QList<T>::operator[](unsigned int index) { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) { if(i==index) return tmp->item; else tmp=tmp->next; } //TODO: Catch error when index is out of range } template<class T> const T& QList<T>::operator[](unsigned int index) const { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) { if(i==index) return tmp->item; else tmp=tmp->next; } //TODO: Catch error when index is out of range } #endif |
Commits for amptest/trunk/QList.h
Revision | Author | Commited | Message |
---|---|---|---|
2 |
![]() |
Wed 20 Jul, 2022 00:52:33 +0000 |