1 #include "forwart_list.h"
2
3
4
5 template<class T>
6 forwart_list<T>::forwart_list():phead(nullptr)
7 {
8
9 }
10
11
12 template<class T>
13 forwart_list<T>::~forwart_list()
14 {
15
16 }
17
18 template<class T>
19 void forwart_list<T>::push_front(T & t)
20 {
21 //开辟节点
22 Node<T> *pnew = new Node<T>;
23 //赋值
24 pnew->t = t;
25 pnew->pNext = nullptr;
26 if (phead == nullptr)
27 {
28 this->phead = pnew;
29 }
30 else
31 {
32 pnew->pNext = phead;
33 this->phead = pnew;
34 }
35 }
36
37 template<class T>
38 void forwart_list<T>::push_front(T && t)
39 {
40 //开辟节点
41 Node<T> *pnew = new Node<T>;
42 //赋值
43 pnew->t = t;
44 pnew->pNext = nullptr;
45 if (phead == nullptr)
46 {
47 this->phead = pnew;
48 }
49 else
50 {
51 pnew->pNext = phead;
52 this->phead = pnew;
53 }
54 }
55
56 template<class T>
57 void forwart_list<T>::push_back(T & t)
58 {
59 //开辟节点
60 Node<T> *pnew = new Node<T>;
61 //赋值
62 pnew->t = t;
63 pnew->pNext = nullptr;
64 if (phead == nullptr)
65 {
66 this->phead = pnew;
67 }
68 else
69 {
70 Node<T>*ptemp = phead;
71 while (ptemp->pNext != nullptr)
72 {
73 ptemp = ptemp->pNext;
74 }
75 ptemp->pNext = pnew;
76 }
77 }
78
79 template<class T>
80 void forwart_list<T>::push_back(T && t)
81 {
82 //开辟节点
83 Node<T> *pnew = new Node<T>;
84 //赋值
85 pnew->t = t;
86 pnew->pNext = nullptr;
87 if (phead == nullptr)
88 {
89 this->phead = pnew;
90 }
91 else
92 {
93 Node<T>*ptemp = phead;
94 while (ptemp->pNext != nullptr)
95 {
96 ptemp = ptemp->pNext;
97 }
98 ptemp->pNext = pnew;
99 }
100 }
101
102 template<class T>
103 void forwart_list<T>::show()
104 {
105 Node<T>*ptemp = phead;
106 while (ptemp != nullptr)
107 {
108 cout << ptemp->t << endl;
109 ptemp = ptemp->pNext;
110 }
111 }
112
113 template<class T>
114 void forwart_list<T>::merge(forwart_list<T>& mylist)
115 {
116 Node<T> *ptemp = phead;
117 while (ptemp->pNext != nullptr)
118 {
119 ptemp = ptemp->pNext;
120 }
121 //归并
122 ptemp->pNext = mylist.phead;
123 }
124
125 template<class T>
126 void forwart_list<T>::insert(T t, T newt)
127 {
128 Node<T> *p = find(t);
129
130 if (p != nullptr)
131 {
132 //p1记录前一个遍历的位置,p2一直遍历
133 Node<T>*p1, *p2;
134 p1 = phead;
135 p2 = phead;
136 while (p2 != p)
137 {
138 p1 = p2;
139 p2 = p2->pNext;
140 }
141
142 Node<T>* pnew = new Node<T>;
143 pnew->t = newt;
144 pnew->pNext = p2;
145 p1->pNext = pnew;
146 }
147 }
148
149 template<class T>
150 Node<T>* forwart_list<T>::find(T t)
151 {
152 Node<T> *ptemp = phead;
153 while (ptemp != nullptr)
154 {
155 if (ptemp->t == t)
156 {
157 return ptemp;
158 }
159 ptemp = ptemp->pNext;
160 }
161 return nullptr;
162 }
163
164 //清空
165 template<class T>
166 void forwart_list<T>::clear()
167 {
168 if (phead == nullptr)
169 {
170 return;
171 }
172 else
173 {
174 //p2保存后一个结点,一直删除p1
175 Node<T>*p1, *p2;
176 p1 = phead;
177 while (p1 != nullptr)
178 {
179 p2 = p1->pNext;
180 delete p1;
181 p1 = nullptr;
182 p1 = p2;
183 }
184 }
185 }
186
187 template<class T>
188 void forwart_list<T>::sort()
189 {
190 for (Node<T>*p1 = head; p1 != nullptr; p1 = p1->pNext)
191 {
192 for (Node<T>*p2 = head; p2->pNext->pNext != nullptr; p2 = p2->pNext)
193 {
194 if (p2->t > p2->pNext->t)
195 {
196 T temp;
197 temp = p1->t;
198 p1->t = p2->t;
199 p2->t = temp;
200 }
201 }
202 }
203 }
204
205 template<class T>
206 void forwart_list<T>::deleteit(T t)
207 {
208 Node<T> *p = find(t);
209
210 if (p != nullptr)
211 {
212 //p1记录前一个遍历的位置,p2一直遍历
213 Node<T>*p1, *p2;
214 p1 = phead;
215 p2 = phead;
216 while (p2 != p)
217 {
218 p1 = p2;
219 p2 = p2->pNext;
220 }
221
222 if (p2 == phead)
223 {
224 phead = p->pNext;
225 delete p;
226 }
227 else
228 {
229 p1->pNext = p2->pNext;
230 delete p2;
231 }
232 p2 = nullptr;
233 }
234 }
235
236 template<class T>
237 int forwart_list<T>::getsize()
238 {
239 int count = 0;
240 Node<T>*p;
241 while (p != nullptr)
242 {
243 p = p->pNext;
244 count++;
245 }
246 return count;
247 }
248
249 template<class T>
250 void forwart_list<T>::change(T t, T newt)
251 {
252 Node<T> *p = find(t);
253 if (p != nullptr)
254 {
255 p -> t = newt;
256 }
257 }
258
259 template<class T>
260 void forwart_list<T>::rev()
261 {
262 if (phead == nullptr || phead->pNext == nullptr)
263 {
264 return;
265 }
266 else
267 {
268 //p1,p2,p3分别是相对第一个位置,相对第二个位置,相对第三个位置
269 Node<T>*p1, *p2, *p3;
270 p1 = phead;
271 p2 = phead->pNext;
272 while (p2 != nullptr)
273 {
274 p3 = p2->pNext;
275 p2->pNext = p1;
276
277 p1 = p2;
278 p2 = p3;
279 }
280 phead->pNext = nullptr;
281 phead = p1;
282 }
283 }
284
285 template<class T>
286 it<T> forwart_list<T>::begin()
287 {
288 return it<T>(this->phead);
289 }
290
291 template<class T>
292 it<T> forwart_list<T>::end()
293 {
294 Node<T>*ptemp = phead;
295 while (ptemp != nullptr)
296 {
297 ptemp = ptemp->pNext;
298 }
299 return it<T>(ptemp);
300 }