緩交作業--double list

  1 #include<iostream>                                                       
  2 #include<string>
  3 #include<sstream>
  4 #include"List.hpp"
  5 using namespace std;
  6 class list;  
  7 std::ostream& operator<<(std::ostream& os, const list& li);
  8 list::list() {
  9     head = NULL;
 10     tail = NULL;
 11     _size = 0;
 12 }            
 13              
 14 list::list(const int _array[], int length) {
 15     int i = 0;
 16     _size = length;
 17     listPointer _find;
 18     for (i = 0; i < length; ++i) {
 19         listPointer _new;
 20         _new = new listNode(_array[i], NULL, NULL);
 21         if (i == 0) {
 22             head = _new;
 23             _find = head;
 24         }    
 25         else {
 26             _find->next = _new;
 27             _new->prev = _find;
 28             if (i == length - 1)
 29                 tail = _new;
 30             _find = _find->next;
 31         }    
 32     }        
 33 }            
 34              
 35 list::list(const list &ls) {
 36     _size = ls._size;
 37     int i = 0;
 38     listPointer _find = ls.head;
 39     if (ls.head) {
 40     for (i = 0; i < _size; ++i) {
 41         listPointer _new;
 42         _new = new listNode(_find->data, NULL, NULL);
 43         if (i == 0) {
 44             head = _new;
 45             tail = _new;
 46         }    
 47         else {
 48             listPointer pre_p = tail;
 49             pre_p->next = _new;
 50             _new->prev = pre_p;
 51             tail = _new;
 52         }    
 53         _find = _find -> next;
 54     }        
 55     }        
 56     else     
 57         head = tail = NULL;
 58 }            
 59              
 60 list& list::operator=(const list &ls) {
 61     if (this)
 62         clear();
 63     _size = ls._size;
 64     int i = 0;
 65     listPointer _find = ls.head;
 66     if (ls.head) {
 67     for (i = 0; i < _size; ++i) {
 68         listPointer _new;
 69         _new = new listNode(_find->data, NULL, NULL);
 70         if (i == 0) {
 71             head = _new;
 72             tail = _new;
 73         }    
 74         else {
 75             listPointer pre_p = tail;
 76             pre_p->next = _new;
 77             _new->prev = pre_p;
 78             tail = _new;
 79         }   
 80         _find = _find -> next;
 81     }       
 82     }       
 83     else    
 84         head = tail = NULL;
 85     return *this;
 86 }    
 87             
 88 list::~list() {
 89     listPointer dele_p;
 90     while (head != tail) {
 91         dele_p = head;
 92         head = head->next;
 93         delete dele_p;
 94     }       
 95     delete tail;
 96     dele_p = head= tail = NULL;
 97     _size = 0;
 98 }
 99             
100 bool list::empty(void) const {
101     return _size == 0;
102 }
103             
104 unsigned int list::size(void) const {
105     return _size;
106 }
107             
108 list::data_type& list::front(void) const {
109     return head->data;
110 }           
111              
112 int& list::back(void) const {
113     return tail->data;
114 }            
115              
116 std::string list::toString(void) const {
117     string result = "NULL";
118     if (_size == 0)
119         return result;
120     else {   
121         listPointer find = head; 
122         for (int i = 0; i < _size; ++i) {
123             if (i == 0) {
124                 stringstream s1;
125                 s1 << find->data; 
126                 result += ("<-" + s1.str());
127             }
128             else {
129                 stringstream s2;
130                 s2 << find->data; 
131                 result += ("<-->" + s2.str());
132             }
133             find = find->next;
134         }    
135     }        
136     result += "->NULL";
137     return result;   
138 }            
139              
140 void list::assign(const list &ls) {
141     this->clear();
142     int i = 0;
143     listPointer _find = ls.head;
144     for (i = 0; i < ls._size; ++i) {
145         push_back(_find->data);
146         _find = _find->next;
147     }        
148     _find = NULL;
149 }            
150              
151 void list:: assign(const int datas[], int length) {
152     this->clear();
153     int i = 0;
154     for (i = 0; i < length; ++i) 
155         push_back(datas[i]);
156 }            
157              
158 void list:: push_front(const int &num) {
159     listPointer _new = new listNode(num ,NULL, NULL);
160     if (head) {
161         head->prev = _new;
162         _new->next = head;
163         head = _new;
164     }        
165     else {   
166         head = _new;
167         tail = _new;  
168     }       
169     ++_size;
170 }           
171             
172 void list:: push_back(const int &num) {
173     listPointer _new = new listNode(num, NULL, NULL);
174     if (tail) {  
175         tail->next = _new;
176         _new->prev = tail;
177         tail = _new;
178     }            
179     else {  
180         head = _new;
181         tail = _new;
182     }       
183     ++_size;     
184 }           
185             
186 void list::pop_front(void) {
187     if (empty())
188         return;
189     else {  
190         if (_size == 1) {
191             delete head;
192             head = NULL;
193             tail = NULL;
194             _size = 0;
195         }   
196         else {
197             listPointer n_p = head->next;
198             delete head;
199             n_p->prev = NULL;
200             head = n_p;
201             --_size;
202         }              
203     }        
204 }            
205              
206 void list::pop_back(void) {
207     if (empty())
208         return;
209     else {   
210         if (_size == 1) {
211             delete tail;
212             tail = NULL;
213             head = NULL;
214         }    
215         else {
216             listPointer l_p = tail->prev;
217             delete tail;
218             l_p->next = NULL;
219             tail = l_p;
220             --_size;
221         }    
222     }        
223 }            
224              
225 void list::insert(int position, const int &data) {
226     if (position > _size && position != 0)
227         return;
228     else {   
229         if (position == 0)
230             push_front(data);
231         else if (position == _size)
232             push_back(data);
233         else {
234             listPointer find_p = at(position);
235             listPointer last_p = at(position - 1);
236             listPointer _new = new listNode(data, find_p, last_p);
237             last_p->next = _new;
238             find_p->prev = _new;
239             ++_size;
240         }    
241     }        
242 }            
243              
244 void list::erase(int position) {
245     if (empty() || position >= _size)
246         return;
247     else {   
248         if (position == 0)
249             pop_front();
250         else if (position == _size - 1)
251             pop_back();
252         else {
253             listPointer find_p = at(position);
254             listPointer last_p = find_p->prev;
255             listPointer next_p = find_p->next;
256             last_p->next = next_p;
257             next_p->prev = last_p;
258             delete find_p;
259             --_size;
260         }    
261     }        
262 }            
263              
264 void list::clear(void) {
265     if (tail) {
266     listPointer dele_p;
267     while (head != tail) {
268         dele_p = head;
269         head = head->next;
270         delete dele_p;
271     }        
272     delete tail;
273     dele_p = head= tail = NULL;
274     _size = 0;
275     }
276 }
277     
278 void list::split(int position, list *des1, list *des2) {
279     int i;  
280     des1->clear();
281     des2->clear();
282     list temp(*this);
283     if (position <= _size) {
284         if (position == 0) {
285             des2->assign(temp);
286             des1->_size = 0;
287             des1->head = des1->tail = NULL;
288         }   
289         else if (position == temp._size) {
290             des1->assign(temp);
291             des2->_size = 0;
292             des2->head = des2->tail = NULL;
293         }
294     
295         else {
296             listPointer find = temp.head;
297             for (i = 0; i < position; ++i) {
298                 des1->push_back(find->data);
299                 find = find->next;
300             }
301             for (i = position; i < temp._size; ++i) {
302                 des2->push_back(find->data);
303                 find = find->next;
304             }
305         }
306     }
307 }     
308              
309 list& list::merge(const list& src1, const list& src2) {
310     list temp(src1);
311     listPointer find = src2.head;
312     for (int i = 0; i < src2._size; ++i) {
313         temp.push_back(find->data);
314         find = find -> next;
315     }        
316     this->assign(temp);
317     return *this;
318 }            
319              
320 list& list::remove_if(bool (*condition)(listPointer)) {
321     int i = 0;
322     listPointer p_find;
323     while (i < _size) {
324         p_find = at(i);
325         if (condition(p_find)) {
326             erase(i);
327         }    
328         else 
329             i++;
330     }        
331     return *this;
332 }            
333              
334 list& list::unique(void) {
335     listPointer p_base;
336     listPointer p_find;
337     int i, j;
338     for (i = 0; i < _size; ++i) {
339         p_base = at(i);
340         j = i + 1;
341         while (j < _size) { 
342             p_find = at(j);
343             if (p_base -> data == p_find -> data)
344                 erase(j);
345             else
346                 ++j;
347         }
348     }       
349     return *this;
350 }    
351             
352 list& list::reverse(void) {
353     listPointer temp;
354     temp = head;
355     head = tail;
356     tail = temp;
357     return *this;
358 }    
359             
360 int& list::operator[](int index) {
361     return at(index)->data;
362 }    
363             
364 list& list::operator+=(const list &ls) {
365     listPointer find = ls.head;
366     for (int i = 0; i < ls._size; i++) {
367         push_back(find->data);
368         find = find->next;
369     }       
370     return *this;    
371 }
372                                                                          
373 std::ostream& operator<<(std::ostream& os, const list& li) {
374     os << li.toString();
375     return os;
376 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值