纸上得来终觉浅,绝知此事要躬行。

 

单链表-数据结构

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace SSS
6 {
7 public class Node
8 {
9 public object element;
10 public Node link;
11
12 public Node()
13 {
14 element = null;
15 link = null;
16
17 }
18
19 public Node(object item)
20 {
21 element = item;
22 this.link = null;
23 }
24
25 }
26
27 public class LinkList
28 {
29 public Node head;
30 public LinkList Next;
31
32 public LinkList()
33 {
34 head = new Node("header");
35 }
36
37 public Node findNode(object item)
38 {
39 Node current = new Node();
40 current=head;
41 while (current.element != item)
42 {
43 current = current.link;
44 }
45 return current;
46
47 }
48
49 public void insertNode(object item,object after)
50 {
51 Node current = new Node();
52 Node newNode=new Node(item);
53 current = findNode(after);
54 if (current != null)
55 {
56 newNode.link = current.link;
57 current.link = newNode;
58 }
59 }
60
61 public void Del(object item)
62 {
63 Node current = new Node();
64 current = findPre(item);
65 Node pre = new Node();
66 if (current != null)
67 current.link = current.link.link;
68 }
69
70 public Node findPre(object item)
71 {
72 Node current = head;
73 while (!(current.link.element != item) && (current.link != null))
74 {
75 current = current.link;
76 }
77 return current;
78 }
79
80 public void PrintList()
81 {
82 Node current = new Node();
83 current = this.head;
84 while (current != null)
85 {
86 Console.WriteLine(current.element);
87 current = current.link;
88 }
89 }
90 }
91
92 class RRR
93 {
94 static void Main(string[] args)
95 {
96 Node firstNode = new Node("firstNode");
97 Node secNode = new Node("secNode");
98 Node thiNode = new Node("11");
99 firstNode.link = secNode;
100 secNode.link = thiNode;
101 thiNode.link = null;
102 LinkList myList = new LinkList();
103 myList.head.link = firstNode;
104 myList.PrintList();
105 myList.insertNode("hanwujibaby","firstNode");
106 myList.PrintList();
107 myList.Del("hanwujibaby");
108 myList.PrintList();
109 System.Threading.Thread.Sleep(8000);
110 }
111 }

 

posted on 2010-08-13 12:08  JRoger  阅读(268)  评论(0编辑  收藏  举报

导航