C#实现单链表

数学不好,不知这是不是理由,又是个何种程度的理由,反正数据结构学的一塌糊涂,尤其是算法复杂度、图形那几部分。为了看懂别人写的代码,自己又拾起数据结构书,啃啃啃!学一点,记一点,希望最后不要是熊子掰玉米,掰一个,丢一个,就OK!

至于单链表的思想就不多说了,直接附代码。

节点:

节点
1 class Node
2 {
3 //假设元素为字符型
4   private String _strName;
5 private Node _objNode;
6 public string StrName
7 {
8 get { return _strName; }
9 set { _strName = value; }
10 }
11 public DataStructure.Node ObjNode
12 {
13 get { return _objNode; }
14 set { _objNode = value; }
15 }
16
17 public Node()
18 {
19 this._strName = null;
20 this._objNode = null;
21 }
22 public Node(String strName)
23 {
24 this._strName = strName;
25 this._objNode = null;
26 }
27 }

 

 

链表的基本功能,增删改查:

 

链表的基本功能
1 class HeadLnkList
2 {
3 //头节点,指向第一个元素
4   private Node _objHead;
5 //判断返回结果真假
6   private Boolean isSuccess;
7
8 public DataStructure.Node ObjHead
9 {
10 get { return _objHead; }
11 set { _objHead = value; }
12 }
13
14 public HeadLnkList()
15 {
16 _objHead = new Node("head");
17 isSuccess = false;
18 }
19
20 /// <summary>
21 /// 查找元素所在的节点
22 /// </summary>
23 /// <param name="strName">待查找的元素</param>
24 /// <param name="outCurrent">保存查找到的节点</param>
25 /// <returns></returns>
26 public Boolean FindCurrent(String strName,out Node outCurrent)
27 {
28 isSuccess = false;
29 Node objCurrent = new Node();
30 objCurrent = _objHead;
31 while((objCurrent!=null)&&(objCurrent.StrName!=strName))
32 {
33 objCurrent = objCurrent.ObjNode;
34 }
35 if (objCurrent == null)
36 {
37 outCurrent = null;
38 }
39 else
40 {
41 isSuccess = true;
42 outCurrent = objCurrent;
43 }
44 return isSuccess;
45 }
46
47 //查找元素所在节点的前一节点
48 public Boolean FindBefore(String strName, out Node outBefore)
49 {
50 isSuccess = false;
51 Node objCurrent = new Node();
52 objCurrent = _objHead;
53 while((objCurrent.ObjNode!=null)&&(objCurrent.ObjNode.StrName!=strName))
54 {
55 objCurrent = objCurrent.ObjNode;
56 }
57 if(objCurrent.ObjNode==null)
58 {
59 outBefore = null;
60 }
61 else
62 {
63 isSuccess = true;
64 outBefore = objCurrent;
65 }
66 return isSuccess;
67 }
68
69 /// <summary>
70 /// 插在指定元素所在节点之后
71 /// </summary>
72 /// <param name="strInsertName">待插入的元素</param>
73 /// <param name="strFindName">指定的元素</param>
74 /// <returns></returns>
75 public Boolean InsertNode(String strInsertName,String strFindName)
76 {
77 isSuccess = false;
78 Node objNewNode = new Node(strInsertName);
79 Node objCurrent = new Node();
80 if (FindCurrent(strFindName, out objCurrent) == true)
81 {
82 //此处开始时写成objNewNode = objCurrent.ObjNode;
83 //总是没结果,就以为编译器的错,哈哈!!!
84 objNewNode.ObjNode = objCurrent.ObjNode;
85 objCurrent.ObjNode = objNewNode;
86 isSuccess = true;
87 }
88 return isSuccess;
89 }
90
91 // 插在指定元素所在节点之前
92 public Boolean InsertBefore(String strInsertName, String strFindName)
93 {
94 isSuccess = false;
95 Node objNewNode = new Node(strInsertName);
96 Node objBefore = new Node();
97 if (FindBefore(strFindName, out objBefore) == true)
98 {
99 objNewNode.ObjNode = objBefore.ObjNode;
100 objBefore.ObjNode = objNewNode;
101 isSuccess = true;
102 }
103 return isSuccess;
104 }
105
106 // 向链表尾部追加节点
107 public Boolean AppendNode(String strName)
108 {
109 isSuccess = false;
110 Node objCurrent=new Node();
111 Node objNewNode = new Node(strName);
112 objCurrent=_objHead;
113 while(objCurrent.ObjNode!=null)
114 {
115 objCurrent = objCurrent.ObjNode;
116 }
117 if (objNewNode.ObjNode == null)
118 {
119 objCurrent.ObjNode = objNewNode;
120 isSuccess = true;
121 }
122 return isSuccess;
123 }
124
125 //移除指定元素的节点
126 public Boolean RemoveNode(String strName)
127 {
128 isSuccess = false;
129 Node objBefore = new Node();
130 if (FindBefore(strName, out objBefore) == true)
131 {
132 objBefore.ObjNode = objBefore.ObjNode.ObjNode;
133 isSuccess = true;
134 }
135 return isSuccess;
136 }
137
138 //打印链表元素
139 public void PrintList()
140 {
141 Node objCurrent = new Node();
142 objCurrent = _objHead;
143 while(objCurrent.ObjNode!=null)
144 {
145 objCurrent = objCurrent.ObjNode;
146 Console.WriteLine(objCurrent.StrName);
147 }
148 }
149 }

 

测试类:

 

 

代码
static void Main(string[] args)
{
TestLnkList();
System.Threading.Thread.Sleep(
3000);
}

private static void TestLnkList()
{
HeadLnkList lnkList
= new HeadLnkList();
lnkList.AppendNode(
"sun");
lnkList.AppendNode(
"moon");
lnkList.AppendNode(
"star");
lnkList.InsertBefore(
"flower", "star");
lnkList.InsertNode(
"tree", "star");
lnkList.PrintList();
Console.WriteLine(
"-------------- remove moon ---------------");
System.Threading.Thread.Sleep(
3000);
lnkList.RemoveNode(
"moon");
lnkList.PrintList();
}

 

感觉指针跳来跳去的,跳的脑袋晕,给自己打打气,加油!

 

posted @ 2010-10-29 17:30  清流鱼  阅读(503)  评论(0编辑  收藏  举报