1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections;
5
6
namespace 单链表
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

2

3

4

5

6

7



8

9



10

11



12

13

14

15

16

17

18

19

20

21

22

23



24

25

26

27

28

29



30

31

32

33

34

35



36

37

38

39

40



41

42

43

44

45

46



47

48

49

50

51

52

53



54

55



56

57

58

59



60

61

62

63

64

65

66



67

68



69

70

71

72



73

74

75

76

77

78

79

80

81



82

83

84

85

86



87

88



89

90

91

92



93

94

95

96

97

98

99



100

101

102

103

104

105



106

107

108

109



110

111

112

113

114

115

116

117

118

119



120

121

122

123

124

125



126

127



128

129

130

131



132

133

134

135

136

137

138



139

140

141

142

143

144

145

146



147

148

149

150



151

152

153

154

155

156



157

158

159

160

161

162

163

164

165



166

167



168

169

170

171

172



173

174

175

176

177

178

179

180

181

182



183

184

185

186

187

188



189

190

191

192

193

194

195

196

197

198

199

200



201

202



203

204

205

206

207



208

209

210

211

212

213

214

215

216



217

218

219

220

221



222

223

224

225

226

227

228

229

230



231

232



233

234

235

236

237

238



239

240

241

242

243

244

245

246

247



248

249

250

251

252

253



254

255

256

257

258



259

260

261

262

263

264

265

266

267



268

269



270

271

272

273

274

275

276

277



278

279

280

281

282

283



284

285

286

287



288

289

290

291

292

293

294

295

296

297



298

299

300



301

302

303

304

305

306

307

308



309

310

311

312

313

314

315

316

317

318

319

320

321

322



323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345



346

347

348

349

350

351

352

353

354

355

356

357

按照例子敲的过程中,对泛型类的实例化花了点功夫,对泛型有了初步认识,在输出单链表时候没有写成方法,希望博友提出改进意见。