数据结构——单链表
001 | using System; |
002 | using System.Collections.Generic; |
003 | using System.Linq; |
004 | using System.Text; |
005 | |
006 | namespace CSDN_Testing |
007 | { |
008 | #region |
009 | //@Author: Peter |
010 | //@Date: 7/26/2010 |
011 | #endregion |
012 | |
013 | #region |
014 | /// <summary> |
015 | /// The definition of the node, and the link is composed of node. |
016 | /// There are two fields in the class, val and next. |
017 | /// </summary> |
018 | public class Node |
019 | { |
020 | private Int32 val; |
021 | private Node next; |
022 | |
023 | public Node() |
024 | { } |
025 | |
026 | public Node(Int32 n) |
027 | { |
028 | this .val = n; |
029 | this .next = null ; |
030 | } |
031 | |
032 | public Int32 Val |
033 | { |
034 | get |
035 | { |
036 | return val; |
037 | } |
038 | set |
039 | { |
040 | val = value; |
041 | } |
042 | } |
043 | |
044 | public Node Next |
045 | { |
046 | get |
047 | { |
048 | return next; |
049 | } |
050 | set |
051 | { |
052 | next = value; |
053 | } |
054 | } |
055 | |
056 | public override bool Equals( object obj) |
057 | { |
058 | return (val == ((Node)obj).val) && (next == ((Node)obj).next); |
059 | } |
060 | |
061 | public override int GetHashCode() |
062 | { |
063 | return base .GetHashCode(); |
064 | } |
065 | } |
066 | |
067 | #endregion |
068 | |
069 | #region |
070 | /// <summary> |
071 | /// The definition of the link list. |
072 | /// There is only one field, it is head, the Head of the link. |
073 | /// The definition contains serval operations on the link, |
074 | /// such as creating a link, add a node to the end, remove a node from link, |
075 | /// put out the information of the link. |
076 | /// </summary> |
077 | public class LinkedList:ICloneable |
078 | { |
079 | private Node head; |
080 | |
081 | public LinkedList() |
082 | { |
083 | head = null ; |
084 | } |
085 | |
086 | public LinkedList( params Int32 [] ns) |
087 | { |
088 | for (Int32 i = 0; i < ns.Length; i++) |
089 | { |
090 | AddNodeToEnd(ns[i]); |
091 | } |
092 | } |
093 | |
094 | //Add a new node at the end of the link |
095 | public void AddNodeToEnd(Int32 n) |
096 | { |
097 | Node newN = new Node(n); |
098 | |
099 | if (head == null ) |
100 | { |
101 | head = newN; |
102 | } |
103 | else |
104 | { |
105 | Node pt = head; |
106 | |
107 | while (pt.Next != null ) |
108 | pt = pt.Next; |
109 | |
110 | pt.Next = newN; |
111 | } |
112 | } |
113 | |
114 | //Return the length of the link |
115 | public Int32 Length |
116 | { |
117 | get |
118 | { |
119 | if (head == null ) |
120 | { |
121 | return 0; |
122 | } |
123 | else |
124 | { |
125 | Int32 len = 0; |
126 | Node pt = head; |
127 | while (pt != null ) |
128 | { |
129 | pt = pt.Next; |
130 | len++; |
131 | } |
132 | return len; |
133 | } |
134 | } |
135 | } |
136 | |
137 | //Insert a new node at the position of index |
138 | public Boolean InsertNodeAt(Int32 index, Int32 val) |
139 | { |
140 | if (index < 1 || index > Length + 1) |
141 | return false ; |
142 | |
143 | if (index == 1) |
144 | { |
145 | Node newN = new Node(val); |
146 | newN.Next = head; |
147 | head = newN; |
148 | } |
149 | else if (index == Length + 1) |
150 | { |
151 | Node pt = head; |
152 | while (pt.Next != null ) |
153 | pt = pt.Next; |
154 | |
155 | pt.Next = new Node(val); |
156 | } |
157 | else |
158 | { |
159 | Node pt = head; |
160 | Int32 i = 1; |
161 | |
162 | while (pt.Next != null ) |
163 | { |
164 | if (i + 1 == index) |
165 | { |
166 | Node newN = new Node(val); |
167 | newN.Next = pt.Next; |
168 | pt.Next = newN; |
169 | break ; |
170 | } |
171 | i++; |
172 | pt = pt.Next; |
173 | } |
174 | } |
175 | |
176 | |
177 | return true ; |
178 | } |
179 | |
180 | //put out the info about the elements in the link and the length |
181 | public void PrintLinkedList() |
182 | { |
183 | Node pt = head; |
184 | while (pt.Next != null ) |
185 | { |
186 | Console.Write(pt.Val + " -> " ); |
187 | pt = pt.Next; |
188 | } |
189 | Console.WriteLine(pt.Val); |
190 | Console.WriteLine( "The lenth of the LinkedList is {0}" , Length); |
191 | } |
192 | |
193 | //Delete the node which value equals v |
194 | public void DeleteNodeByValue(Int32 v) |
195 | { |
196 | if (Length == 0) |
197 | return ; |
198 | |
199 | if (head.Val == v) |
200 | { |
201 | head = head.Next; |
202 | return ; |
203 | } |
204 | else |
205 | { |
206 | Node pt = head; |
207 | while (pt.Next != null ) |
208 | { |
209 | if (pt.Next.Val == v) |
210 | { |
211 | pt.Next = pt.Next.Next; |
212 | break ; |
213 | } |
214 | pt = pt.Next; |
215 | } |
216 | } |
217 | } |
218 | |
219 | //Reverse the link |
220 | public void ReverseLink() |
221 | { |
222 | if (head == null || head.Next == null ) |
223 | return ; |
224 | |
225 | Node pre = head; |
226 | Node cur = head.Next; |
227 | if (cur.Next == null ) |
228 | { |
229 | cur.Next = pre; |
230 | pre.Next = null ; |
231 | head = cur; |
232 | } |
233 | else |
234 | { |
235 | Node next = cur.Next; |
236 | |
237 | while (next != null ) |
238 | { |
239 | cur.Next = pre; |
240 | pre = cur; |
241 | cur = next; |
242 | next = next.Next; |
243 | } |
244 | cur.Next = pre; |
245 | head.Next = null ; |
246 | head = cur; |
247 | } |
248 | } |
249 | |
250 | //Sort the link by ascending |
251 | public void Sort() |
252 | { |
253 | Node cur = head; |
254 | if (head == null || head.Next == null ) |
255 | return ; |
256 | |
257 | Node next = cur.Next; |
258 | while (next != null ) |
259 | { |
260 | Node pt = head; |
261 | Node ptPre = head; |
262 | while (!pt.Equals(next)) |
263 | { |
264 | if (next.Val < pt.Val) |
265 | { |
266 | if (pt.Equals(ptPre) && pt.Equals(head)) |
267 | { |
268 | cur.Next = next.Next; |
269 | next.Next = head; |
270 | head = next; |
271 | } |
272 | else |
273 | { |
274 | cur.Next = next.Next; |
275 | next.Next = pt; |
276 | ptPre.Next = next; |
277 | } |
278 | |
279 | break ; |
280 | } |
281 | ptPre = pt; |
282 | pt = pt.Next; |
283 | } |
284 | |
285 | cur = next; |
286 | next = next.Next; |
287 | } |
288 | } |
289 | |
290 | //Merge two link, the result is stored in the first link in order. |
291 | //no influence on the second link |
292 | public void Merge(LinkedList link2) |
293 | { |
294 | Node pt = head; |
295 | |
296 | LinkedList temp = (LinkedList)link2.Clone(); |
297 | while (pt.Next != null ) |
298 | pt = pt.Next; |
299 | |
300 | pt.Next = temp.head; |
301 | |
302 | this .Sort(); |
303 | } |
304 | |
305 | public object Clone() |
306 | { |
307 | Node pt = head; |
308 | if (head == null ) |
309 | return null ; |
310 | |
311 | LinkedList link = new LinkedList(); |
312 | while (pt != null ) |
313 | { |
314 | link.AddNodeToEnd(pt.Val); |
315 | pt = pt.Next; |
316 | } |
317 | return link; |
318 | } |
319 | |
320 | } |
321 | |
322 | #endregion |
323 | |
324 | class Program |
325 | { |
326 | static void Main( string [] args) |
327 | { |
328 | |
329 | LinkedList link = new LinkedList(1, 3, 15, 4, 5); |
330 | |
331 | Console.WriteLine( "Construct a new link:" ); |
332 | link.PrintLinkedList(); |
333 | Console.WriteLine(); |
334 | |
335 | Console.WriteLine( "Insert a node which is 0 at index:1 " ); |
336 | link.InsertNodeAt(1,0); |
337 | link.PrintLinkedList(); |
338 | Console.WriteLine(); |
339 | |
340 | Console.WriteLine( "Insert a node which is 6 at index:7" ); |
341 | link.InsertNodeAt(7,6); |
342 | link.PrintLinkedList(); |
343 | Console.WriteLine(); |
344 | |
345 | Console.WriteLine( "Insert a node which is 7 at index:7" ); |
346 | link.InsertNodeAt(7,7); |
347 | link.PrintLinkedList(); |
348 | |
349 | Console.WriteLine( "Delete the first node which vaule is 0 from start" ); |
350 | link.DeleteNodeByValue(0); |
351 | link.PrintLinkedList(); |
352 | Console.WriteLine(); |
353 | |
354 | Console.WriteLine( "Delete the first node which vaule is 6 from start" ); |
355 | link.DeleteNodeByValue(6); |
356 | link.PrintLinkedList(); |
357 | Console.WriteLine(); |
358 | |
359 | Console.WriteLine( "Delete the first node which vaule is 3 from start" ); |
360 | link.DeleteNodeByValue(3); |
361 | link.PrintLinkedList(); |
362 | Console.WriteLine(); |
363 | |
364 | Console.WriteLine( "Reverse the link" ); |
365 | link.ReverseLink(); |
366 | link.PrintLinkedList(); |
367 | Console.WriteLine(); |
368 | |
369 | Console.WriteLine( "Sort the link by ascending" ); |
370 | link.Sort(); |
371 | link.PrintLinkedList(); |
372 | Console.WriteLine(); |
373 | |
374 | LinkedList link2 = new LinkedList(11,22); |
375 | |
376 | Console.WriteLine( "Merge the link2 to the link and store result in link by ascending" ); |
377 | link.Merge(link2); |
378 | link.PrintLinkedList(); |
379 | Console.WriteLine( "put out the info of link2, confirm the merger has no influence on link2" ); |
380 | link2.PrintLinkedList(); |
381 | |
382 | } |
383 | } |
384 | } |