1using System;
2using System.Collections;
3using System.Collections.Generic;
4namespace MyLinkList
5{
6 public class LNode<T> //结点
7 {
8 private T data; //结点值
9 private LNode<T> nextNode;
10 private LNode<T> prevNode;
11 public LNode()
12 {
13
14 }
15 public T Data
16 {
17 get { return data; }
18 set { data = value; }
19 }
20 public LNode(LNode<T> prNode,LNode<T> nexNode,T dat)
21 {
22 data = dat;
23 nextNode = nexNode;
24 prevNode=prNode;
25 }
26 public LNode<T> NextNode
27 {
28 get{ return nextNode; }
29 set { nextNode = value; }
30 }
31 public LNode<T> PreviousNode
32 {
33 get { return prevNode; }
34 set { prevNode = value; }
35 }
36
37 }
38 public class LkList<T> : IEnumerable<T>//链表
39 {
40 LNode<T> Current; //当前结点
41 private LNode<T> Head; //头结点
42 private int NodeCount=0; //结点数目
43 private int currentIndex = 0; //当前结点索引位置
44
45 public LkList()
46 {
47 Current = null;
48 Head = Current;
49 }
50 public int CurrentIndex
51 {
52 get { return currentIndex; }
53 set { currentIndex = value; }
54 }
55 public LNode<T> CurrentNode
56 {
57 get { return Current; }
58 }
59 public void Add(T DataValue)
60 {
61 //添加结点
62 LNode<T> newNode = new LNode<T>(null,null,DataValue);
63 if (NodeCount == 0)
64 {
65 Current = newNode;
66 Head = Current;
67 NodeCount++;
68 }
69 else
70 {
71 Current.NextNode = newNode;
72 newNode.PreviousNode = Current;
73 Current = newNode;
74 NodeCount++;
75 }
76 currentIndex++;
77
78
79 }
80 public void Del(int i)
81 {
82 //i 为索引参数
83 if (i < 0 || i > this.GetLength())
84 {
85 Console.WriteLine("not exist data!");
86 }
87 else
88 {
89 this.MoveToindex(i);
90 LNode<T> temp = Current.PreviousNode;
91 temp.NextNode = Current.NextNode;
92 NodeCount--;
93 Current = Head;
94 currentIndex = 1;
95
96 }
97 }
98 public void Insert(int i,T data)
99 {
100 //在第i个元素后添加结点
101
102 this.MoveToindex(i);
103 LNode<T> temp = Current.NextNode;
104 this.Add(data);
105 Current.NextNode = temp;
106
107 }
108 public void Next()
109 {
110 //取下个结点
111 if (Current.NextNode == null)
112 Console.WriteLine("out of index");
113 else
114 Current = Current.NextNode;
115 currentIndex++;
116 }
117 public void Previous()
118 {
119 //取上个结点
120 if (Current.PreviousNode == null)
121 {
122 Console.WriteLine("out of index");
123 }
124 else
125 {
126 Current = Current.PreviousNode;
127 currentIndex--;
128 }
129 }
130 public void MoveToindex(int nodeIndex)
131 {
132 //跳转到第i个结点
133 if (nodeIndex < 0 || nodeIndex > this.GetLength())
134 {
135 Console.WriteLine("out of index!\n");
136 }
137 else
138 {
139 while (currentIndex > nodeIndex)
140 {
141 Previous();
142 }
143 while (currentIndex < nodeIndex)
144 {
145 Next();
146 }
147
148 }
149
150 }
151 public T GetByindex(int nodeIndex)
152 {
153 if (nodeIndex < 0 || nodeIndex > this.GetLength())
154 {
155 Console.WriteLine("out of index!\n");
156 }
157 else
158 MoveToindex(nodeIndex);
159 return Current.Data;
160
161 }
162 public int GetLength()
163 {
164 //获取结点数目
165 LNode<T> temp = Current;
166 int listlength = 0;
167 Current = Head;
168 while (Current !=null)
169 {
170 Current = Current.NextNode;
171 listlength++;
172 }
173 Current = temp;
174 return listlength;
175 }
176 public void Display()
177 {
178 //显示链表
179 LNode<T> temp = Current;
180 Current = Head;
181 Console.Write("The list data is:");
182 while (Current != null)
183 {
184 Console.Write("{0}--->",Current.Data);
185 Current = Current.NextNode;
186
187 }
188 Current = temp;
189 }
190 public IEnumerator<T> GetEnumerator()
191 {
192 // 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
193 // foreach 迭代。请注意,在 C# 2.0 中,
194 // 不需要实现 Current 和 MoveNext。
195 // 编译器将创建实现 IEnumerator<T> 的类。
196 LNode<T> temp = Current;
197 Current = Head;
198 while (Current != null)
199 {
200 yield return Current.Data;
201 Current = Current.NextNode;
202 }
203 Current = temp;
204 }
205 IEnumerator IEnumerable.GetEnumerator()
206 {
207
208 return GetEnumerator();
209 }
210
211
212
213 }
214 public class test
215 {
216 //当然是泛型,你可以用字符串去做结点
217 public static void Main()
218 {
219 string value;
220 int intvalue;
221 LkList<int> mylist = new LkList<int>();
222 Console.WriteLine("input three nodes which type is int");
223
224 value = Console.ReadLine();
225 intvalue = Convert.ToInt32(value);
226 mylist.Add(intvalue);
227 value = Console.ReadLine();
228 intvalue = Convert.ToInt32(value);
229 mylist.Add(intvalue);
230 value = Console.ReadLine();
231 intvalue = Convert.ToInt32(value);
232 mylist.Add(intvalue);
233 mylist.Display();
234
235 //可以用foreach迭代
236 //foreach (int a in mylist)
237 //{
238 // Console.WriteLine(a);
239 //}
240 Console.WriteLine("\n");
241 Console.WriteLine("the length of list is:" + mylist.GetLength());
242 LNode<int> t = mylist.CurrentNode;
243
244 Console.WriteLine("this is current index is:");
245 Console.WriteLine(mylist.CurrentIndex);
246
247 mylist.Del(2);
248 Console.WriteLine("the list after delete the 2th node:");
249 mylist.Display();
250 Console.WriteLine("\n");
251 Console.WriteLine("insert a node after the 2th node");
252 intvalue = Convert.ToInt32(Console.ReadLine());
253 mylist.Insert(2, intvalue);
254 mylist.Display();
255 Console.Write("\n");
256 Console.Write("GetByindex method return the 2th node data value:");
257 Console.Write(mylist.GetByindex(2).ToString());
258
259
260 Console.ReadLine();
261
262 }
263 }
264
265
266}
267
学数据结构,顺便用C#2.0的泛型实现链表.功能较少,代码也很笨拙的说,但链表基本功能实现,并实现IEnumerable<T>接口.测试是用INT类型测试,当然你也可以用STRING类型
2using System.Collections;
3using System.Collections.Generic;
4namespace MyLinkList
5{
6 public class LNode<T> //结点
7 {
8 private T data; //结点值
9 private LNode<T> nextNode;
10 private LNode<T> prevNode;
11 public LNode()
12 {
13
14 }
15 public T Data
16 {
17 get { return data; }
18 set { data = value; }
19 }
20 public LNode(LNode<T> prNode,LNode<T> nexNode,T dat)
21 {
22 data = dat;
23 nextNode = nexNode;
24 prevNode=prNode;
25 }
26 public LNode<T> NextNode
27 {
28 get{ return nextNode; }
29 set { nextNode = value; }
30 }
31 public LNode<T> PreviousNode
32 {
33 get { return prevNode; }
34 set { prevNode = value; }
35 }
36
37 }
38 public class LkList<T> : IEnumerable<T>//链表
39 {
40 LNode<T> Current; //当前结点
41 private LNode<T> Head; //头结点
42 private int NodeCount=0; //结点数目
43 private int currentIndex = 0; //当前结点索引位置
44
45 public LkList()
46 {
47 Current = null;
48 Head = Current;
49 }
50 public int CurrentIndex
51 {
52 get { return currentIndex; }
53 set { currentIndex = value; }
54 }
55 public LNode<T> CurrentNode
56 {
57 get { return Current; }
58 }
59 public void Add(T DataValue)
60 {
61 //添加结点
62 LNode<T> newNode = new LNode<T>(null,null,DataValue);
63 if (NodeCount == 0)
64 {
65 Current = newNode;
66 Head = Current;
67 NodeCount++;
68 }
69 else
70 {
71 Current.NextNode = newNode;
72 newNode.PreviousNode = Current;
73 Current = newNode;
74 NodeCount++;
75 }
76 currentIndex++;
77
78
79 }
80 public void Del(int i)
81 {
82 //i 为索引参数
83 if (i < 0 || i > this.GetLength())
84 {
85 Console.WriteLine("not exist data!");
86 }
87 else
88 {
89 this.MoveToindex(i);
90 LNode<T> temp = Current.PreviousNode;
91 temp.NextNode = Current.NextNode;
92 NodeCount--;
93 Current = Head;
94 currentIndex = 1;
95
96 }
97 }
98 public void Insert(int i,T data)
99 {
100 //在第i个元素后添加结点
101
102 this.MoveToindex(i);
103 LNode<T> temp = Current.NextNode;
104 this.Add(data);
105 Current.NextNode = temp;
106
107 }
108 public void Next()
109 {
110 //取下个结点
111 if (Current.NextNode == null)
112 Console.WriteLine("out of index");
113 else
114 Current = Current.NextNode;
115 currentIndex++;
116 }
117 public void Previous()
118 {
119 //取上个结点
120 if (Current.PreviousNode == null)
121 {
122 Console.WriteLine("out of index");
123 }
124 else
125 {
126 Current = Current.PreviousNode;
127 currentIndex--;
128 }
129 }
130 public void MoveToindex(int nodeIndex)
131 {
132 //跳转到第i个结点
133 if (nodeIndex < 0 || nodeIndex > this.GetLength())
134 {
135 Console.WriteLine("out of index!\n");
136 }
137 else
138 {
139 while (currentIndex > nodeIndex)
140 {
141 Previous();
142 }
143 while (currentIndex < nodeIndex)
144 {
145 Next();
146 }
147
148 }
149
150 }
151 public T GetByindex(int nodeIndex)
152 {
153 if (nodeIndex < 0 || nodeIndex > this.GetLength())
154 {
155 Console.WriteLine("out of index!\n");
156 }
157 else
158 MoveToindex(nodeIndex);
159 return Current.Data;
160
161 }
162 public int GetLength()
163 {
164 //获取结点数目
165 LNode<T> temp = Current;
166 int listlength = 0;
167 Current = Head;
168 while (Current !=null)
169 {
170 Current = Current.NextNode;
171 listlength++;
172 }
173 Current = temp;
174 return listlength;
175 }
176 public void Display()
177 {
178 //显示链表
179 LNode<T> temp = Current;
180 Current = Head;
181 Console.Write("The list data is:");
182 while (Current != null)
183 {
184 Console.Write("{0}--->",Current.Data);
185 Current = Current.NextNode;
186
187 }
188 Current = temp;
189 }
190 public IEnumerator<T> GetEnumerator()
191 {
192 // 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
193 // foreach 迭代。请注意,在 C# 2.0 中,
194 // 不需要实现 Current 和 MoveNext。
195 // 编译器将创建实现 IEnumerator<T> 的类。
196 LNode<T> temp = Current;
197 Current = Head;
198 while (Current != null)
199 {
200 yield return Current.Data;
201 Current = Current.NextNode;
202 }
203 Current = temp;
204 }
205 IEnumerator IEnumerable.GetEnumerator()
206 {
207
208 return GetEnumerator();
209 }
210
211
212
213 }
214 public class test
215 {
216 //当然是泛型,你可以用字符串去做结点
217 public static void Main()
218 {
219 string value;
220 int intvalue;
221 LkList<int> mylist = new LkList<int>();
222 Console.WriteLine("input three nodes which type is int");
223
224 value = Console.ReadLine();
225 intvalue = Convert.ToInt32(value);
226 mylist.Add(intvalue);
227 value = Console.ReadLine();
228 intvalue = Convert.ToInt32(value);
229 mylist.Add(intvalue);
230 value = Console.ReadLine();
231 intvalue = Convert.ToInt32(value);
232 mylist.Add(intvalue);
233 mylist.Display();
234
235 //可以用foreach迭代
236 //foreach (int a in mylist)
237 //{
238 // Console.WriteLine(a);
239 //}
240 Console.WriteLine("\n");
241 Console.WriteLine("the length of list is:" + mylist.GetLength());
242 LNode<int> t = mylist.CurrentNode;
243
244 Console.WriteLine("this is current index is:");
245 Console.WriteLine(mylist.CurrentIndex);
246
247 mylist.Del(2);
248 Console.WriteLine("the list after delete the 2th node:");
249 mylist.Display();
250 Console.WriteLine("\n");
251 Console.WriteLine("insert a node after the 2th node");
252 intvalue = Convert.ToInt32(Console.ReadLine());
253 mylist.Insert(2, intvalue);
254 mylist.Display();
255 Console.Write("\n");
256 Console.Write("GetByindex method return the 2th node data value:");
257 Console.Write(mylist.GetByindex(2).ToString());
258
259
260 Console.ReadLine();
261
262 }
263 }
264
265
266}
267