|
| 1 | + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | + # .---. .----------- |
| 3 | + # / \ __ / ------ |
| 4 | + # / / \( )/ ----- (`-') _ _(`-') <-. (`-')_ |
| 5 | + # ////// '\/ ` --- ( OO).-/( (OO ).-> .-> \( OO) ) .-> |
| 6 | + # //// / // : : --- (,------. \ .'_ (`-')----. ,--./ ,--/ ,--.' ,-. |
| 7 | + # // / / / `\/ '-- | .---' '`'-..__)( OO).-. ' | \ | | (`-')'.' / |
| 8 | + # // //..\\ (| '--. | | ' |( _) | | | | . '| |)(OO \ / |
| 9 | + # ============UU====UU==== | .--' | | / : \| |)| | | |\ | | / /) |
| 10 | + # '//||\\` | `---. | '-' / ' '-' ' | | \ | `-/ /` |
| 11 | + # ''`` `------' `------' `-----' `--' `--' `--' |
| 12 | + # ######################################################################################################## |
| 13 | + # |
| 14 | + # Author: edony - edonyzpc@gmail.com |
| 15 | + # |
| 16 | + # twitter : @edonyzpc |
| 17 | + # |
| 18 | + # Last modified: 2014-12-02 13:40 |
| 19 | + # |
| 20 | + # Filename: dou_list.cpp |
| 21 | + # |
| 22 | + # Description: All Rights Are Reserved |
| 23 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 24 | +#include "dou_list.h" |
| 25 | +using namespace myalg; |
| 26 | +using namespace std; |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +dl_mem<T>::dl_mem():l_mem<T>() |
| 30 | +{ |
| 31 | + this->pre = NULL; |
| 32 | + this->next = NULL; |
| 33 | +} |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +dl_mem<T> dl_mem<T>::operator=(dl_mem<T> assi) |
| 37 | +{ |
| 38 | + dl_mem<T> tmp; |
| 39 | + tmp.setdata(assi.getdata()); |
| 40 | + tmp.setpos(assi.getpos()); |
| 41 | + tmp.setnext(assi.getnext()); |
| 42 | + tmp.setpre(assi.getpre()); |
| 43 | + return tmp; |
| 44 | +} |
| 45 | + |
| 46 | +template <typename T1> |
| 47 | +dou_list<T1>::dou_list():linklist<T1>() |
| 48 | +{ |
| 49 | + this->head = new dl_mem<T1>; |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +template <typename T1> |
| 54 | +dou_list<T1>::dou_list(dou_list &cp):linklist<T1>() |
| 55 | +{ |
| 56 | + this->size = cp.getsize(); |
| 57 | + *(this->head) = *(cp.head); |
| 58 | + *(this->tail) = *(cp.tail); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +template <typename T1> |
| 63 | +int dou_list<T1>::rm_pre(dl_mem<T1> *rm_pos) |
| 64 | +{ |
| 65 | + if(rm_pos == NULL || rm_pos->getpre() ==NULL) return -1;//error removement |
| 66 | + dl_mem<T1> * del = rm_pos->getpre(); |
| 67 | + dl_mem<T1> * _del =del->getpre(); |
| 68 | + cout<<del<<"|"<<_del<<endl; |
| 69 | + rm_pos->setpre(_del); |
| 70 | + _del->setnext(rm_pos); |
| 71 | + this->size--; |
| 72 | + dl_mem<T1> * tmp = rm_pos; |
| 73 | + int i = 0; |
| 74 | + while(tmp != NULL) |
| 75 | + { |
| 76 | + tmp->setpos(tmp->getpos() - 1); |
| 77 | + tmp = tmp->getnext(); |
| 78 | + std::cout<<"loop:"<<++i<<std::endl; |
| 79 | + } |
| 80 | + delete del; |
| 81 | + return 0;//finish removement |
| 82 | +} |
| 83 | + |
| 84 | +template <typename T1> |
| 85 | +dl_mem<T1>* dou_list<T1>::find_mem(T1 find_data) |
| 86 | +{ |
| 87 | + dl_mem<T1> * tmp = this->head; |
| 88 | + if(tmp == NULL) return NULL; |
| 89 | + |
| 90 | + tmp = tmp->getnext(); |
| 91 | + while(tmp != NULL) |
| 92 | + { |
| 93 | + if(tmp->getdata() != find_data) |
| 94 | + { |
| 95 | + cout<<tmp->getdata()<<endl; |
| 96 | + tmp = tmp->getnext(); |
| 97 | + continue; |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + return tmp; |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +template <typename T1> |
| 109 | +dou_list<T1>::~dou_list() |
| 110 | +{ |
| 111 | + delete head; |
| 112 | + delete tail; |
| 113 | +} |
| 114 | + |
| 115 | +template<typename T1> |
| 116 | +void dou_list<T1>::print_list() |
| 117 | +{ |
| 118 | + dl_mem<T1>* tmp = this->head->getnext(); |
| 119 | + int s = this->size; |
| 120 | + std::cout<<"head"<<std::endl; |
| 121 | + std::cout<<" |"<<std::endl; |
| 122 | + while(s > 0) |
| 123 | + { |
| 124 | + T1 out = tmp->getdata(); |
| 125 | + int position = tmp->getpos(); |
| 126 | + std::cout<<out<<"--"<<position<<"pre "<<tmp->getnext()<<" now "<<tmp<<" next "<<tmp->getnext()<<std::endl; |
| 127 | + tmp = tmp->getnext(); |
| 128 | + s--; |
| 129 | + } |
| 130 | + std::cout<<" |"<<std::endl; |
| 131 | + std::cout<<"tail"<<std::endl; |
| 132 | +} |
| 133 | + |
| 134 | +template<typename T1> |
| 135 | +int dou_list<T1>::linklist_ins_next(T1 ins_data, dl_mem<T1> *ins_pos) |
| 136 | +{ |
| 137 | + dl_mem<T1> *ins = new dl_mem<T1>; |
| 138 | + if(ins == NULL) return -1; |
| 139 | + ins->setdata(ins_data); |
| 140 | + |
| 141 | + if(ins_pos == NULL)//insert at the head for default |
| 142 | + { |
| 143 | + ins->setpre(this->head); |
| 144 | + if(this->head->getnext() == NULL) |
| 145 | + { |
| 146 | + this->head->setnext(ins); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + ins->setnext(this->head->getnext()); |
| 151 | + this->head->setnext(ins); |
| 152 | + this->head->getnext()->setpre(ins); |
| 153 | + } |
| 154 | + ins->setpos(this->head->getpos() + 1); |
| 155 | + l_mem<T1> *poschange = ins->getnext(); |
| 156 | + while(poschange != NULL) |
| 157 | + { |
| 158 | + poschange->setpos(poschange->getpos() + 1); |
| 159 | + poschange = poschange->getnext(); |
| 160 | + } |
| 161 | + this->size++; |
| 162 | + if(this->size == 1) |
| 163 | + { |
| 164 | + this->tail = ins; |
| 165 | + ins->setnext(NULL); |
| 166 | + } |
| 167 | + this->tail->setpos(this->size); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + ins->setpre(ins_pos); |
| 172 | + if(ins_pos->getnext() == NULL) |
| 173 | + { |
| 174 | + ins->setnext(NULL); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + ins->setnext(ins_pos->getnext()); |
| 179 | + } |
| 180 | + ins_pos->setnext(ins); |
| 181 | + ins_pos->getnext()->setpre(ins); |
| 182 | + ins->setpos(ins_pos->getpos() + 1); |
| 183 | + l_mem<T1> *poschange = ins->getnext(); |
| 184 | + while(poschange != NULL) |
| 185 | + { |
| 186 | + poschange->setpos(poschange->getpos() + 1); |
| 187 | + poschange = poschange->getnext(); |
| 188 | + } |
| 189 | + this->size++; |
| 190 | + if(ins->getnext() == NULL) |
| 191 | + { |
| 192 | + this->tail = ins; |
| 193 | + this->tail->setnext(NULL); |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + this->tail->setpos(this->size); |
| 198 | + this->tail->setnext(NULL); |
| 199 | + } |
| 200 | + } |
| 201 | + return 0; |
| 202 | + |
| 203 | +} |
| 204 | +template <typename T1> |
| 205 | +int dou_list<T1>::linklist_ins_pre(T1 ins_data, dl_mem<T1> *ins_pos) |
| 206 | +{ |
| 207 | + dl_mem<T1> *ins = new dl_mem<T1>; |
| 208 | + if(ins == NULL) return -1; |
| 209 | + ins->setdata(ins_data); |
| 210 | + |
| 211 | + if(ins_pos == NULL)//insert at the head for default |
| 212 | + { |
| 213 | + ins->setpre(this->head); |
| 214 | + if(this->head->getnext() == NULL) |
| 215 | + { |
| 216 | + this->head->setnext(ins); |
| 217 | + ins->setnext(NULL); |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + ins->setnext(this->head->getnext()); |
| 222 | + this->head->setnext(ins); |
| 223 | + this->head->getnext()->setpre(ins); |
| 224 | + } |
| 225 | + ins->setpos(this->head->getpos() + 1); |
| 226 | + dl_mem<T1> *poschange = ins->getnext(); |
| 227 | + while(poschange != NULL) |
| 228 | + { |
| 229 | + poschange->setpos(poschange->getpos() + 1); |
| 230 | + poschange = poschange->getnext(); |
| 231 | + } |
| 232 | + this->size++; |
| 233 | + if(this->size == 1) |
| 234 | + { |
| 235 | + this->tail = ins; |
| 236 | + ins->setnext(NULL); |
| 237 | + } |
| 238 | + this->tail->setpos(this->size); |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + ins->setnext(ins_pos); |
| 243 | + ins->setpre(ins_pos->getpre()); |
| 244 | + ins_pos->getpre()->setnext(ins); |
| 245 | + ins_pos->setpre(ins); |
| 246 | + ins->setpos(ins_pos->getpos()); |
| 247 | + dl_mem<T1> *poschange = ins->getnext(); |
| 248 | + while(poschange != NULL) |
| 249 | + { |
| 250 | + poschange->setpos(poschange->getpos() + 1); |
| 251 | + poschange = poschange->getnext(); |
| 252 | + } |
| 253 | + this->size++; |
| 254 | + if(ins->getnext() == NULL) |
| 255 | + { |
| 256 | + this->tail = ins; |
| 257 | + this->tail->setnext(NULL); |
| 258 | + } |
| 259 | + else |
| 260 | + { |
| 261 | + this->tail->setpos(this->size); |
| 262 | + this->tail->setnext(NULL); |
| 263 | + } |
| 264 | + } |
| 265 | + return 0; |
| 266 | +} |
0 commit comments