1 #include 2 3 using namespace std; 4 5 class A; 6 7 template 8 class ListNode 9 { 10 public: 11 //explicit ListNode(); 12 ListNode(); 13 ~ListNode(); 14 ListNode *next; 15 Node info; 16 }; 17 18 template 19 ListNode ::ListNode() 20 { 21 22 } 23 24 template 25 ListNode ::~ListNode() 26 { 27 } 28 29 class A 30 { 31 public: 32 A(); 33 A(int a); 34 A(const A& a); 35 ~A(); 36 bool operator ==(const A& a1); 37 int id; 38 39 }; 40 41 A::A() 42 { 43 } 44 45 A::A(int a ) 46 { 47 id = a ; 48 49 } 50 51 A::A(const A& a) 52 { 53 id = a.id; 54 55 cout<<"test"< 70 class List 71 { 72 public: 73 List(); 74 ~List(); 75 void insert(const T& item); 76 void insert(const T& item,int pos); 77 void append(const T& item); 78 ListNode *reverse(); 79 //int find(const T& item); 80 int find_first(const T& item); 81 int find_end(const T& item); 82 // remove(const T& item); 83 bool remove(int pos); 84 85 void printList(); 86 void printList(ListNode *list); 87 88 ListNode< T> *head; 89 int position ; 90 }; 91 92 template 93 List ::List() 94 { 95 head = NULL; 96 position = 0; 97 } 98 99 template 100 List ::~List()101 {102 }103 104 template 105 void List ::insert(const T& item)106 {107 108 ListNode *listNode= new ListNode ();109 listNode->next = head;110 listNode->info = item;111 112 head = listNode;113 114 position +=1;115 }116 117 template 118 void List ::insert(const T& item,int pos)119 {120 if (position<-1 || pos >position )121 {122 return;123 }124 125 if(pos ==0)126 {127 insert(item);128 }129 else130 {131 ListNode *tmp = head;132 133 for (int i = 0; i < pos; i++)134 {135 tmp = tmp->next;136 }137 138 ListNode *node = tmp->next;139 140 ListNode *listNode= new ListNode ();141 listNode->next = node;142 listNode->info = item;143 144 tmp->next = listNode;145 146 position +=1;147 }148 }149 150 template 151 void List ::append(const T& item)152 {153 if(head !=NULL)154 {155 ListNode *tmp = head;156 157 while(tmp->next !=NULL)158 {159 tmp = tmp->next;160 }161 162 ListNode *listNode= new ListNode ();163 listNode->next = NULL;164 listNode->info = item;165 166 tmp->next = listNode;167 168 position +=1;169 }170 else171 {172 insert(item);173 }174 }175 176 /*template 177 void List ::remove(const T& item)178 {179 ListNode *tmp = head;180 181 while(tmp !=NULL)182 {183 if(tmp->info == item)184 {185 186 }187 188 tmp = tmp->next;189 }190 }*/191 192 template 193 bool List ::remove(int pos)194 {195 if(head == NULL || pos<-1 || pos>position)196 return false;197 198 if(head !=NULL)199 {200 ListNode *currentNode = head;201 202 int i = 0;203 204 while(currentNode !=NULL)205 {206 i++;207 208 if(i == pos)209 break;210 211 currentNode = currentNode->next;212 }213 214 ListNode *tmp = currentNode->next;215 216 currentNode->next = tmp->next->next;217 218 delete tmp;219 tmp = NULL;220 221 position-=1;222 223 }224 }225 226 template 227 int List ::find_first(const T& item)228 {229 if(head != NULL)230 {231 ListNode *currentNode = head;232 int i = 0;233 234 while(currentNode !=NULL)235 {236 i++;237 if(currentNode->info == item)238 {239 cout< - < next;244 }245 246 return i;247 248 }else249 {250 return -1;251 }252 }253 254 template 255 int List ::find_end(const T& item)256 {257 if(head != NULL)258 {259 ListNode *currentNode = head;260 int i = 0;261 int pos = 0;262 263 while(currentNode !=NULL)264 {265 i++;266 if(currentNode->info == item)267 {268 pos = i;269 }270 271 currentNode = currentNode->next;272 }273 274 return pos;275 276 }else277 {278 return -1;279 }280 }281 282 template 283 ListNode *List ::reverse()284 {285 if(head !=NULL) 286 {287 ListNode *tmp = head;288 ListNode *newList = new ListNode ();289 newList->info = tmp->info;290 newList->next = NULL;291 292 ListNode *swap;293 294 while(tmp->next !=NULL)295 {296 swap = newList->next;297 newList->next = tmp->next;298 tmp->next = tmp->next->next;299 newList->next->next = swap;300 }301 302 return newList;303 304 }305 else306 {307 return NULL;308 }309 }310 311 312 template 313 void List ::printList()314 {315 316 ListNode *tmp = head;317 318 while(tmp !=NULL)319 {320 cout< info< next;322 }323 }324 325 template 326 void List ::printList(ListNode *list)327 {328 329 ListNode *tmp = list;330 331 while(tmp !=NULL)332 {333 cout< info< next;335 }336 }337 338 339 int main()340 {341 List *list = new List ;342 343 for (int i = 0; i < 10; i++)344 {345 list->insert(i);346 }347 348 list->insert(12,0);349 list->insert(0,2);350 list->insert(5,3);351 list->insert(5,7);352 353 list->append(122);354 list->append(130);355 356 ListNode *node = new ListNode ();357 358 ListNode *tmp = node;359 360 cout<<"find first = "< find_first(5)<
*tmplist = list->reverse();372 373 list->printList(tmplist);374 375 376 delete list;377 378 379 List
*alist = new List ;380 381 for(int i=0;i<5;i++)382 {383 A a(i);384 alist->insert(a);385 }386 }