1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Collections;
5
6namespace 单链表
7{
8 class Program
9 {
10 public interface IListDS<T>
11 {
12 int GetLength(); //获取表长度
13 void Clear(); //清空
14 bool IsEmpty(); //判断表是否为空
15 void Append(T item); //附加操作
16 void Insert(T item, int i); //插入
17 T Delete(int i); //删除
18 T GetElem(int i); //取表元
19 int Locate(T value); //按值查找
20
21 }
22 public class Node<T>
23 {
24 private T data; //数据域
25 private Node<T> next; //引用域
26
27 //构造器
28 public Node(T val, Node<T> P)
29 {
30 data = val;
31 next = P;
32 }
33 //构造器
34 public Node(Node<T> P)
35 {
36 next = P;
37 }
38 //构造器
39 public Node(T val)
40 {
41 data = val;
42 next = null;
43 }
44 //构造器
45 public Node()
46 {
47 data = default(T);
48 next = null;
49 }
50
51 //数据域属性
52 public T Data
53 {
54 get
55 {
56 return data;
57 }
58 set
59 {
60 data = value;
61 }
62 }
63
64 //引用域
65 public Node<T> Next
66 {
67 get
68 {
69 return next;
70 }
71 set
72 {
73 next = value;
74 }
75 }
76
77
78 }
79
80 public class LinkList<T> : IListDS<T>
81 {
82 //单链表头引用
83 private Node<T> head;
84 //头引用属性
85 public Node<T> Head
86 {
87 get
88 {
89 return head;
90 }
91 set
92 {
93 head = value;
94 }
95 }
96
97 //构造器
98 public LinkList()
99 {
100 head = null;
101 }
102
103 //求单链表长度
104 public int GetLength()
105 {
106 Node<T> P = head;
107 int len = 0;
108 while (P != null)
109 {
110 ++len;
111 P = P.Next;
112
113 }
114 return len;
115 }
116
117 //清空
118 public void Clear()
119 {
120 head = null;
121 }
122
123 //判断单链表是否为空
124 public bool IsEmpty()
125 {
126 if (head == null)
127 {
128 return true;
129 }
130 else
131 {
132 return false;
133 }
134 }
135
136 //插入头结点
137 public void InsertHead(T item)
138 {
139 head = new Node<T>(item,head);
140 }
141
142
143
144 //单链表末尾添加新元素
145 public void Append(T item)
146 {
147 Node<T> q = new Node<T>(item);
148 Node<T> p=new Node<T>();
149 if (head == null)
150 {
151 head = q;
152 return;
153 }
154 p = head;
155 while (p.Next != null)
156 {
157 p = p.Next;
158 }
159 p.Next = q;
160
161 }
162
163 //在单链表的第i个节点的位置前插入一个值为item的节点
164 public void Insert(T item, int i)
165 {
166 if (IsEmpty() || i < 1)
167 {
168 Console.WriteLine("表为空!");
169 return;
170 }
171 if (i == 1)
172 {
173 Node<T> q = new Node<T>(item);
174 q.Next = head;
175 head = q;
176 return;
177 }
178 Node<T> p = head;
179 Node<T> r=new Node<T>();
180 int j = 1;
181 while (p.Next != null && j < i)
182 {
183 r = p;
184 p = p.Next;
185 ++j;
186 }
187 if (j == i)
188 {
189 Node<T> q = new Node<T>(item);
190 q.Next = p;
191 r.Next = q;
192 }
193
194 }
195
196
197
198 //在单链表的第i个节点的位置后插入一个值为item的节点
199 public void InsertPost(T item, int i)
200 {
201 if (IsEmpty() || i < 1)
202 {
203 Console.WriteLine("表为空!");
204 return;
205 }
206 if (i == 1)
207 {
208 Node<T> q = new Node<T>(item);
209 q.Next = head.Next;
210 head.Next = q;
211 return;
212 }
213 Node<T> p = head;
214 int j = 1;
215 while (p != null && j < i)
216 {
217 p = p.Next;
218 ++j;
219 }
220 if (j == i)
221 {
222 Node<T> q = new Node<T>(item);
223 q.Next = p.Next;
224 p.Next = q;
225 }
226 }
227
228 //删除单链表第i个节点
229 public T Delete(int i)
230 {
231 if (IsEmpty() || i < 1)
232 {
233 Console.WriteLine("表为空!");
234 return default(T);
235 }
236 Node<T> q = new Node<T>();
237 if (i == 1)
238 {
239 q = head;
240 head = head.Next;
241 return q.Data;
242
243 }
244 Node<T> p = head;
245 int j = 1;
246 while (p.Next != null && j < i)
247 {
248 ++j;
249 q = p;
250 p = p.Next;
251 }
252 if (j == i)
253 {
254 q.Next = p.Next;
255 return p.Data;
256 }
257 else
258 {
259 Console.WriteLine("这个节点不存在!");
260 return default(T);
261 }
262
263 }
264
265 //获得单链表第i个数据元素
266 public T GetElem(int i)
267 {
268 if (IsEmpty() || i < 1)
269 {
270 Console.WriteLine("表为空!");
271 return default(T);
272 }
273 Node<T> p = new Node<T>();
274 p = head;
275 int j = 1;
276 while (p.Next != null && j < i)
277 {
278 ++j;
279 p = p.Next;
280 }
281
282 if (j == i)
283 {
284 return p.Data;
285 }
286 else
287 {
288 Console.WriteLine("这个节点不存在!");
289 return default(T);
290
291 }
292
293 }
294
295 //在单链表中查找值为value的元素
296 public int Locate(T value)
297 {
298
299 if (IsEmpty())
300 {
301 Console.WriteLine("表为空!");
302 return -1;
303 }
304 Node<T> p = new Node<T>();
305 p = head;
306 int i = 1;
307 while (!p.Data.Equals(value) && p.Next != null)
308 {
309 p = p.Next;
310 ++i;
311 }
312 return i;
313
314 }
315
316
317
318
319 }
320
321 static void Main(string[] args)
322 {
323 //创建firstnode,secnode,thirdnode三个结点,并赋予对应的值
324 Node<int> firstnode=new Node<int>(1);
325 Node<int> secnode = new Node<int>(2);
326 Node<int> thirdnode = new Node<int>(3);
327 //指定结点之间的连接顺序
328 firstnode.Next = secnode;
329 secnode.Next = thirdnode;
330 thirdnode.Next = null;
331 //实例化LinkList
332 LinkList<int> list = new LinkList<int>();
333 //指定头Head
334 list.Head = firstnode;
335 //在第3个元素之前插入值4
336 list.Insert(4,3);
337 //在第4个元素之后插入值5
338 list.InsertPost(5,4);
339 //末尾插入值6
340 list.Append(6);
341 //输出单链表中的数据域
342 Node<int> current = new Node<int>();
343 current =list.Head;
344 while (current != null)
345 {
346 Console.WriteLine(current.Data);
347 current = current.Next;
348 }
349
350 //查找值为5的位置
351 Console.WriteLine(list.Locate(5));
352
353
354 }
355 }
356}
357
2using System.Collections.Generic;
3using System.Text;
4using System.Collections;
5
6namespace 单链表
7{
8 class Program
9 {
10 public interface IListDS<T>
11 {
12 int GetLength(); //获取表长度
13 void Clear(); //清空
14 bool IsEmpty(); //判断表是否为空
15 void Append(T item); //附加操作
16 void Insert(T item, int i); //插入
17 T Delete(int i); //删除
18 T GetElem(int i); //取表元
19 int Locate(T value); //按值查找
20
21 }
22 public class Node<T>
23 {
24 private T data; //数据域
25 private Node<T> next; //引用域
26
27 //构造器
28 public Node(T val, Node<T> P)
29 {
30 data = val;
31 next = P;
32 }
33 //构造器
34 public Node(Node<T> P)
35 {
36 next = P;
37 }
38 //构造器
39 public Node(T val)
40 {
41 data = val;
42 next = null;
43 }
44 //构造器
45 public Node()
46 {
47 data = default(T);
48 next = null;
49 }
50
51 //数据域属性
52 public T Data
53 {
54 get
55 {
56 return data;
57 }
58 set
59 {
60 data = value;
61 }
62 }
63
64 //引用域
65 public Node<T> Next
66 {
67 get
68 {
69 return next;
70 }
71 set
72 {
73 next = value;
74 }
75 }
76
77
78 }
79
80 public class LinkList<T> : IListDS<T>
81 {
82 //单链表头引用
83 private Node<T> head;
84 //头引用属性
85 public Node<T> Head
86 {
87 get
88 {
89 return head;
90 }
91 set
92 {
93 head = value;
94 }
95 }
96
97 //构造器
98 public LinkList()
99 {
100 head = null;
101 }
102
103 //求单链表长度
104 public int GetLength()
105 {
106 Node<T> P = head;
107 int len = 0;
108 while (P != null)
109 {
110 ++len;
111 P = P.Next;
112
113 }
114 return len;
115 }
116
117 //清空
118 public void Clear()
119 {
120 head = null;
121 }
122
123 //判断单链表是否为空
124 public bool IsEmpty()
125 {
126 if (head == null)
127 {
128 return true;
129 }
130 else
131 {
132 return false;
133 }
134 }
135
136 //插入头结点
137 public void InsertHead(T item)
138 {
139 head = new Node<T>(item,head);
140 }
141
142
143
144 //单链表末尾添加新元素
145 public void Append(T item)
146 {
147 Node<T> q = new Node<T>(item);
148 Node<T> p=new Node<T>();
149 if (head == null)
150 {
151 head = q;
152 return;
153 }
154 p = head;
155 while (p.Next != null)
156 {
157 p = p.Next;
158 }
159 p.Next = q;
160
161 }
162
163 //在单链表的第i个节点的位置前插入一个值为item的节点
164 public void Insert(T item, int i)
165 {
166 if (IsEmpty() || i < 1)
167 {
168 Console.WriteLine("表为空!");
169 return;
170 }
171 if (i == 1)
172 {
173 Node<T> q = new Node<T>(item);
174 q.Next = head;
175 head = q;
176 return;
177 }
178 Node<T> p = head;
179 Node<T> r=new Node<T>();
180 int j = 1;
181 while (p.Next != null && j < i)
182 {
183 r = p;
184 p = p.Next;
185 ++j;
186 }
187 if (j == i)
188 {
189 Node<T> q = new Node<T>(item);
190 q.Next = p;
191 r.Next = q;
192 }
193
194 }
195
196
197
198 //在单链表的第i个节点的位置后插入一个值为item的节点
199 public void InsertPost(T item, int i)
200 {
201 if (IsEmpty() || i < 1)
202 {
203 Console.WriteLine("表为空!");
204 return;
205 }
206 if (i == 1)
207 {
208 Node<T> q = new Node<T>(item);
209 q.Next = head.Next;
210 head.Next = q;
211 return;
212 }
213 Node<T> p = head;
214 int j = 1;
215 while (p != null && j < i)
216 {
217 p = p.Next;
218 ++j;
219 }
220 if (j == i)
221 {
222 Node<T> q = new Node<T>(item);
223 q.Next = p.Next;
224 p.Next = q;
225 }
226 }
227
228 //删除单链表第i个节点
229 public T Delete(int i)
230 {
231 if (IsEmpty() || i < 1)
232 {
233 Console.WriteLine("表为空!");
234 return default(T);
235 }
236 Node<T> q = new Node<T>();
237 if (i == 1)
238 {
239 q = head;
240 head = head.Next;
241 return q.Data;
242
243 }
244 Node<T> p = head;
245 int j = 1;
246 while (p.Next != null && j < i)
247 {
248 ++j;
249 q = p;
250 p = p.Next;
251 }
252 if (j == i)
253 {
254 q.Next = p.Next;
255 return p.Data;
256 }
257 else
258 {
259 Console.WriteLine("这个节点不存在!");
260 return default(T);
261 }
262
263 }
264
265 //获得单链表第i个数据元素
266 public T GetElem(int i)
267 {
268 if (IsEmpty() || i < 1)
269 {
270 Console.WriteLine("表为空!");
271 return default(T);
272 }
273 Node<T> p = new Node<T>();
274 p = head;
275 int j = 1;
276 while (p.Next != null && j < i)
277 {
278 ++j;
279 p = p.Next;
280 }
281
282 if (j == i)
283 {
284 return p.Data;
285 }
286 else
287 {
288 Console.WriteLine("这个节点不存在!");
289 return default(T);
290
291 }
292
293 }
294
295 //在单链表中查找值为value的元素
296 public int Locate(T value)
297 {
298
299 if (IsEmpty())
300 {
301 Console.WriteLine("表为空!");
302 return -1;
303 }
304 Node<T> p = new Node<T>();
305 p = head;
306 int i = 1;
307 while (!p.Data.Equals(value) && p.Next != null)
308 {
309 p = p.Next;
310 ++i;
311 }
312 return i;
313
314 }
315
316
317
318
319 }
320
321 static void Main(string[] args)
322 {
323 //创建firstnode,secnode,thirdnode三个结点,并赋予对应的值
324 Node<int> firstnode=new Node<int>(1);
325 Node<int> secnode = new Node<int>(2);
326 Node<int> thirdnode = new Node<int>(3);
327 //指定结点之间的连接顺序
328 firstnode.Next = secnode;
329 secnode.Next = thirdnode;
330 thirdnode.Next = null;
331 //实例化LinkList
332 LinkList<int> list = new LinkList<int>();
333 //指定头Head
334 list.Head = firstnode;
335 //在第3个元素之前插入值4
336 list.Insert(4,3);
337 //在第4个元素之后插入值5
338 list.InsertPost(5,4);
339 //末尾插入值6
340 list.Append(6);
341 //输出单链表中的数据域
342 Node<int> current = new Node<int>();
343 current =list.Head;
344 while (current != null)
345 {
346 Console.WriteLine(current.Data);
347 current = current.Next;
348 }
349
350 //查找值为5的位置
351 Console.WriteLine(list.Locate(5));
352
353
354 }
355 }
356}
357
按照例子敲的过程中,对泛型类的实例化花了点功夫,对泛型有了初步认识,在输出单链表时候没有写成方法,希望博友提出改进意见。