1 // 2 // Created by yinus on 2016/4/2 3 // 4 #ifndef DS_AA_LIST_H 5 #define DS_AA_LIST_H 6 #include <algorithm> 7 using std::ostream; 8 template <typename Object> 9 class List{ 10 private: 11 struct Node{ 12 Object data; 13 14 Node *prev; 15 16 Node *next; 17 18 Node(const Object& d=Object{},Node* p = nullptr,Node* n = nullptr):data(d),prev(p),next(n){} 19 20 Node(Object &&d,Node* p = nullptr,Node* n = nullptr):data(std::move(d)),prev(p),next(n){} 21 22 }; 23 public: 24 class const_iterator{ 25 public: 26 const_iterator():current(nullptr) { } 27 28 const Object& operator*() const{ return retrieve(); } 29 30 const_iterator& operator++() { 31 current=current->next; 32 return *this; 33 } 34 35 const_iterator operator++(int){ 36 const_iterator old = *this; 37 ++(*this); 38 return old; 39 } 40 41 bool operator==(const const_iterator& rhs) const{ 42 return current==rhs.current; 43 } 44 45 bool operator!=(const const_iterator& rhs) const{ 46 return !(*this==rhs); 47 } 48 49 protected: 50 Node *current; 51 52 Object& retrieve() const { return current->data; } 53 54 const_iterator(Node *p):current(p){} 55 56 friend class List<Object>; 57 58 }; 59 60 class iterator:public const_iterator{ 61 public: 62 iterator(){} 63 Object& operator*(){ 64 return const_iterator::retrieve(); 65 } 66 67 const Object& operator*()const { 68 return const_iterator::operator*(); 69 } 70 71 iterator& operator++(){ 72 this->current=this->current->next; 73 return *this; 74 } 75 76 iterator operator++(int){ 77 iterator old = *this; 78 ++(*this); 79 return old; 80 } 81 82 83 84 protected: 85 iterator(Node* p):const_iterator(p){} 86 friend class List<Object>; 87 88 }; 89 90 public: 91 List() { 92 init(); 93 } 94 95 List(const List & rhs){ 96 init(); 97 for (const_iterator itr = rhs.begin(); itr!=rhs.end() ; itr++) { 98 push_back(itr.current->data); 99 } 100 } 101 102 List(List &&rhs):theSize(rhs.theSize),head(rhs.head),tail(rhs.tail) { 103 rhs.theSize=0; 104 rhs.head= nullptr; 105 rhs.tail= nullptr; 106 } 107 108 ~List() { 109 clear(); 110 delete head; 111 delete tail; 112 } 113 114 List& operator=(const List &rhs) { 115 List copy = rhs; 116 std::swap(*this,copy); 117 return *this; 118 } 119 120 iterator begin() { 121 return head->next; 122 } 123 124 const_iterator begin() const { 125 return head->next; 126 } 127 128 iterator end(){ 129 return tail; 130 } 131 132 const_iterator end() const { 133 return tail; 134 } 135 136 int size() const { 137 return theSize; 138 } 139 140 bool empty() const { 141 return size()==0; 142 } 143 144 void clear() { 145 while(!empty()) 146 pop_front(); 147 } 148 149 Object & front() { 150 return begin().current->data; 151 } 152 153 const Object & front() const { 154 return begin().current->data; 155 } 156 157 Object & back() { 158 return end().current->prev->data; 159 } 160 161 const Object & back()const { 162 return end().current->prev->data; 163 } 164 165 void push_front(const Object &x) { 166 insert(begin(),x); 167 } 168 169 void push_front(Object &&x) { 170 insert(begin(),std::move(x)); 171 } 172 173 void push_back(const Object &x) { 174 insert(end(),x); 175 } 176 177 void push_back(Object &&x) { 178 insert(end(),std::move(x)); 179 } 180 181 void pop_front() { 182 erase(begin()); 183 } 184 185 void pop_back(){ 186 erase(end()); 187 } 188 189 iterator insert(iterator itr, const Object & x) { 190 Node *p = itr.current; 191 theSize++; 192 return p->prev=p->prev->next=new Node{x,p->prev,p}; 193 } 194 195 iterator insert(iterator itr, Object && x) { 196 Node *p = itr.current; 197 theSize++; 198 return p->prev=p->prev->next=new Node{std::move(x),p->prev,p}; 199 } 200 201 void print() 202 { 203 for (iterator itr = begin(); itr !=end() ; ++itr) { 204 std::cout<<itr.current->data<<" "; 205 } 206 } 207 208 iterator erase(iterator itr) { 209 Node *p = itr.current; 210 if(p->next== nullptr) 211 p=p->prev; 212 iterator retVal(p->next); 213 p->prev->next=p->next; 214 p->next->prev=p->prev; 215 delete p; 216 theSize--; 217 return retVal; 218 } 219 220 iterator erase(iterator from,iterator to) { 221 for (iterator itr = from; itr!=to ; ) 222 itr = erase(itr); 223 return to; 224 } 225 226 private: 227 int theSize; 228 Node *head; 229 Node *tail; 230 void init() { 231 theSize=0; 232 head = new Node; 233 tail = new Node; 234 head->next=tail; 235 tail->prev=head; 236 } 237 }; 238 #endif //DS_AA_LIST_H