1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 class A;
  6 
  7 template<class Node>
  8 class ListNode
  9 {
 10     public:
 11         //explicit ListNode();
 12         ListNode();
 13         ~ListNode();
 14         ListNode *next;
 15         Node info;
 16 };
 17 
 18 template<class Node>
 19 ListNode<Node>::ListNode()
 20 {
 21     
 22 }
 23 
 24 template<class Node>
 25 ListNode<Node>::~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"<<endl;
 56 
 57 }
 58 
 59 A::~A()
 60 {
 61 
 62 }
 63 
 64 bool A::operator ==(const A& a1)
 65 {
 66 
 67 }
 68 
 69 template<class T >
 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<T> *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<T> *list);
 87 
 88         ListNode< T> *head;
 89         int position ;
 90 };
 91 
 92 template<class T>
 93 List<T>::List()
 94 {
 95     head = NULL;
 96     position = 0;
 97 }
 98 
 99 template<class T>
100 List<T>::~List()
101 {
102 }
103 
104 template<class T>
105 void List<T>::insert(const T& item)
106 {
107 
108     ListNode<T> *listNode= new ListNode<T>();
109     listNode->next = head;
110     listNode->info = item;
111 
112     head = listNode;
113 
114     position +=1;
115 }
116 
117 template<class T>
118 void List<T>::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     else
130     {
131         ListNode<T> *tmp = head;
132 
133         for (int i = 0; i < pos; i++)
134         {
135             tmp = tmp->next;
136         }
137 
138         ListNode<T> *node = tmp->next;
139 
140         ListNode<T> *listNode= new ListNode<T>();
141         listNode->next = node;
142         listNode->info = item;
143 
144         tmp->next = listNode;
145 
146         position +=1;
147     }
148 }
149 
150 template<class T>
151 void List<T>::append(const T& item)
152 {
153     if(head !=NULL)
154     {
155         ListNode<T> *tmp = head;
156 
157         while(tmp->next !=NULL)
158         {
159             tmp = tmp->next;
160         }
161 
162         ListNode<T> *listNode= new ListNode<T>();
163         listNode->next = NULL;
164         listNode->info = item;
165 
166         tmp->next = listNode;
167 
168         position +=1;
169     }
170     else
171     {
172         insert(item);
173     }
174 }
175 
176 /*template<class T>
177 void List<T>::remove(const T& item)
178 {
179     ListNode<T> *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<class T>
193 bool List<T>::remove(int pos)
194 {
195     if(head == NULL || pos<-1 || pos>position)
196         return false;
197 
198     if(head !=NULL)
199     {
200         ListNode<T> *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<T> *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<class T>
227 int List<T>::find_first(const T& item)
228 {
229     if(head != NULL)
230     {
231         ListNode<T> *currentNode = head;
232         int i = 0;
233 
234         while(currentNode !=NULL)
235         {
236             i++;
237             if(currentNode->info == item)
238             {
239                 cout<<item<<endl;
240                 break;
241             }
242 
243             currentNode = currentNode->next;
244     }
245 
246         return i;
247 
248     }else
249     {
250         return -1;
251     }
252 }
253 
254 template<class T>
255 int List<T>::find_end(const T& item)
256 {
257     if(head != NULL)
258     {
259         ListNode<T> *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     }else
277     {
278         return -1;
279     }
280 }
281 
282 template<class T>
283 ListNode<T> *List<T>::reverse()
284 {
285     if(head !=NULL)    
286     {
287         ListNode<T> *tmp = head;
288         ListNode<T> *newList  =  new ListNode<T>();
289         newList->info = tmp->info;
290         newList->next = NULL;
291 
292         ListNode<T> *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     else
306     {
307         return NULL;
308     }
309 }
310 
311 
312 template<class T>
313 void List<T>::printList()
314 {
315 
316     ListNode<T> *tmp = head;
317 
318     while(tmp !=NULL)
319     {
320         cout<<tmp->info<<endl;
321         tmp = tmp->next;
322     }
323 }
324 
325 template<class T>
326 void List<T>::printList(ListNode<T> *list)
327 {
328 
329     ListNode<T> *tmp = list;
330 
331     while(tmp !=NULL)
332     {
333         cout<<tmp->info<<endl;
334         tmp = tmp->next;
335     }
336 }
337 
338 
339 int main()
340 {
341     List<int> *list = new List<int>;
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<int> *node = new ListNode<int>();
357 
358     ListNode<int> *tmp = node;
359 
360     cout<<"find first = "<<list->find_first(5)<<endl;
361     cout<<"find end = "<<list->find_end(5)<<endl;
362 
363     list->remove(5);
364     list->remove(6);
365 
366     list->printList();
367 
368     cout<<"*******************************************************"<<endl;
369 
370 
371     ListNode<int> *tmplist = list->reverse();
372 
373     list->printList(tmplist);
374 
375 
376     delete list;
377 
378 
379     List<A> *alist = new List<A>;
380 
381     for(int i=0;i<5;i++)
382     {
383         A a(i);
384         alist->insert(a);
385     }
386 }

以上是使用模板实现的数据结构