c++ 模板 链表实现

  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 }

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

转载于:https://www.cnblogs.com/jjxxjnzy/p/3430417.html

面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值