C#实现单链表(线性表)完整实例

时间:2022-03-30 05:20:14

本文实例讲述了C#实现单链表(线性表)的方法。分享给大家供大家参考,具体如下:

顺序表由连续内存构成,链表则不同。顺序表的优势在于查找,链表的优势在于插入元素等操作。顺序表的例子:http://www.zzvips.com/article/208158.html

要注意的是,单链表的Add()方法最好不要频繁调用,尤其是链表长度较长的时候,因为每次Add,都会从头节点到尾节点进行遍历,这个缺点的优化方法是将节点添加到头部,但顺序是颠倒的。

所以,在下面的例子中,执行Purge(清洗重复元素)的时候,没有使用Add()方法去添加元素,而是定义一个节点,让它始终指向目标单链表的最后一个节点,这样就不用每次都从头到尾遍历。

此外,链表还可以做成循环链表,即最后一个结点的next属性等于head,主要操作与单链表相似,判断最后一个结点,不是等于null,而是等于head

?
1
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinearList
{
  //定义线性表的行为,可供顺序表类和单链表类继承
  public interface IListDS<T>
  {
    int GetLength();
    void Insert(T item, int i);
    void Add(T item);
    bool IsEmpty();
    T GetElement(int i);
    void Delete(int i);
    void Clear();
    int LocateElement(T item);
    void Reverse();
  }
  //链表节点类
  class Node<T>
  {
    private T tData;
    private Node<T> nNext;
    public T Data
    {
      get { return this.tData; }
      set { this.tData = value; }
    }
    public Node<T> Next
    {
      get { return this.nNext; }
      set { this.nNext = value; }
    }
    public Node()
    {
      this.tData = default(T);
      this.nNext = null;
    }
    public Node(T t)
    {
      this.tData = t;
      this.nNext = null;
    }
    public Node(T t,Node<T> node)
    {
      this.tData = t;
      this.nNext = node;
    }
  }
  //该枚举表示单链表Add元素的位置,分头部和尾部两种
  enum AddPosition {Head,Tail};
  //单链表类
  class LinkedList<T>:IListDS<T>
  {
    private Node<T> tHead;//单链表的表头
    public Node<T> Head
    {
      get { return this.tHead; }
      set { this.tHead = value; }
    }
    public LinkedList()
    {
      this.tHead = null;
    }
    public LinkedList(Node<T> node)
    {
      this.tHead = node;
    }
    public void Add(T item,AddPosition p)//选择添加位置
    {
      if (p == AddPosition.Tail)
      {
        this.Add(item);//默认添加在末尾
      }
      else//从头部添加会节省查找的开销,时间复杂度为O(1)不必每次都循环到尾部,这恰好是顺序表的优点
      {
        Node<T> node = this.Head;
        Node<T> nodeTmp = new Node<T>(item);
        if (node == null)
        {
          this.Head = nodeTmp;
        }
        else
        {
          nodeTmp.Next = node;
          this.tHead = nodeTmp;
        }
      }
    }
    #region IListDS<T> 成员
    public int GetLength()
    {
      Node<T> node = new Node<T>();
      int count = 0;
      node = this.tHead;
      while (node != null)
      {
        count++;
        node = node.Next;
      }
      return count;
    }
    public void Insert(T item, int i)//i最小从1开始
    {
      Node<T> insertNode = new Node<T>(item, null);//实例化待添加的Node
      if (this.tHead == null && i == 1)
      {
        this.tHead = insertNode;
        return;
      }
      if (i < 1 || i > this.GetLength() || (this.tHead == null && i != 1))
      {
        Console.WriteLine("There are no elements in this linked list!");
        return;
      }
      int j = 1;
      Node<T> node = this.tHead;
      Node<T> nodeTmp;
      while (node != null && j < i)//循环结束时,保证node为第i个node
      {
        node = node.Next;
        j++;
      }
      nodeTmp = node.Next;//原来的单链表的第i+1个node
      node.Next = insertNode;//第i个node后的node修改为待插入的node
      insertNode.Next = nodeTmp;//待插入的node插入后,其后继node为原来链表的第i+1个node
    }
    public void Add(T item)//添加至尾部,时间复杂度为O(n),如果添加至头部,则会节省循环的开销
    {
      Node<T> LastNode = new Node<T>(item, null);//实例化待添加的Node
      if (this.tHead == null)
      {
        this.tHead = LastNode;
      }
      else
      {
        Node<T> node = this.tHead;
        while (node.Next != null)
        {
          node = node.Next;
        }
        node.Next = LastNode;
      }
    }
    public bool IsEmpty()
    {
      return this.tHead == null;
    }
    public T GetElement(int i)//设i最小从1开始
    {
      if (i < 1 || i > this.GetLength())
      {
        Console.WriteLine("The location is not right!");
        return default(T);
      }
      else
      {
        if (i == 1)
        {
          return this.tHead.Data;
        }
        else
        {
          Node<T> node = this.tHead;
          int j = 1;
          while (j < i)
          {
            node = node.Next;
            j++;
          }
          return node.Data;
        }
      }
    }
    public void Delete(int i)//设i最小从1开始
    {
      if (i < 1 || i > this.GetLength())
      {
        Console.WriteLine("The location is not right!");
      }
      else
      {
        if (i == 1)
        {
          Node<T> node = this.tHead;
          this.tHead = node.Next;
        }
        else
        {
          Node<T> node = this.tHead;
          int j = 1;
          while (j < i-1)
          {
            node = node.Next;
            j++;
          }
          node.Next = node.Next.Next;
        }
      }
    }
    public void Clear()
    {
      this.tHead = null;//讲thead设为null后,则所有后继结点由于失去引用,等待GC自动回收
    }
    public int LocateElement(T item)//返回值最小从1开始
    {
      if (this.tHead == null)
      {
        Console.WriteLine("There are no elements in this linked list!");
        return -1;
      }
      Node<T> node = this.tHead;
      int i = 0;
      while (node != null)
      {
        i++;
        if (node.Data.Equals(item))//如果Data是自定义类型,则其Equals函数必须override
        {
          return i;
        }
        node = node.Next;
      }
      Console.WriteLine("No found!");
      return -1;
    }
    public void Reverse()
    {
      if (this.tHead == null)
      {
        Console.WriteLine("There are no elements in this linked list!");
      }
      else
      {
        Node<T> node = this.tHead;
        if (node.Next == null)//如果只有头节点,则不作任何改动
        {
        }
        else
        {
          Node<T> node1 = node.Next;
          Node<T> node2;
          while (node1 != null)
          {
            node2 = node.Next.Next;
            node.Next = node2;//可以发现node始终未变,始终是原来的那个头节点
            node1.Next = this.tHead;
            this.tHead = node1;
            node1 = node2;
          }
        }
      }
    }
    #endregion
  }
  class Program
  {
    static void Main(string[] args)
    {
      /*测试单链表的清空
      lList.Clear();
      Node<int> n = new Node<int>();
      n = lList.Head;
      while (n != null)
      {
        Console.WriteLine(n.Data);
        n = n.Next;
      }
      Console.ReadLine();
       */
      /*测试单链表返回元素个数
      LinkedList<int> lList = new LinkedList<int>();
      lList.Add(3);
      Console.WriteLine(lList.GetLength());
      Console.ReadLine();
      */
      /*测试单链表插入
      LinkedList<int> lList = new LinkedList<int>();
      lList.Insert(0,1);
      lList.Add(1);
      lList.Add(2);
      lList.Add(3);
      lList.Add(4);
      lList.Insert(99,3);
      Node<int> n = new Node<int>();
      n = lList.Head;
      while (n != null)
      {
        Console.WriteLine(n.Data);
        n = n.Next;
      }
      Console.ReadLine();
      */
      /*测试单链表获取某位置的值
      LinkedList<int> lList = new LinkedList<int>();
      lList.Add(1);
      lList.Add(2);
      lList.Add(3);
      lList.Add(4);
      Console.WriteLine(lList.GetElement(1));
      Console.ReadLine();
       */
      /*测试单链表删除某位置的值
      LinkedList<int> lList = new LinkedList<int>();
      lList.Add(1);
      lList.Add(2);
      lList.Add(3);
      lList.Add(4);
      Node<int> n = new Node<int>();
      n = lList.Head;
      while (n != null)
      {
        Console.WriteLine(n.Data);
        n = n.Next;
      }
      Console.ReadLine();
      lList.Delete(2);
      Node<int> m = new Node<int>();
      m = lList.Head;
      while (m != null)
      {
        Console.WriteLine(m.Data);
        m = m.Next;
      }
      Console.ReadLine();
      */
       /*测试单链表按值查找元素位置
      LinkedList<int> lList = new LinkedList<int>();
      lList.Add(1);
      lList.Add(2);
      lList.Add(3);
      lList.Add(4);
      Console.WriteLine(lList.LocateElement(3));
      Console.ReadLine();
      */
      /*测试单链表Reverse操作(逆序)
      LinkedList<int> lList = new LinkedList<int>();
      lList.Add(1);
      lList.Add(2);
      lList.Add(3);
      lList.Add(4);
      lList.Add(5);
      Node<int> m = new Node<int>();
      m = lList.Head;
      while (m != null)
      {
        Console.WriteLine(m.Data);
        m = m.Next;
      }
      Console.ReadLine();
      lList.Reverse();
      Node<int> n = new Node<int>();
      n = lList.Head;
      while (n != null)
      {
        Console.WriteLine(n.Data);
        n = n.Next;
      }
      Console.ReadLine();
      */
      /*测试单链表从头部添加元素
      LinkedList<int> lList = new LinkedList<int>();
      lList.Add(1,AddPosition.Head);
      lList.Add(2, AddPosition.Head);
      lList.Add(3, AddPosition.Head);
      lList.Add(4, AddPosition.Head);
      lList.Add(5, AddPosition.Head);
      Node<int> m = new Node<int>();
      m = lList.Head;
      while (m != null)
      {
        Console.WriteLine(m.Data);
        m = m.Next;
      }
      Console.ReadLine();
      */
      /*测试对单链表清除重复元素操作(返回另一链表)。这个例子中避免使用Add()方法,因为每个Add()都要从头到尾进行遍历,不适用Add()方法
       就要求对目标链表的最后一个元素实时保存。另一种避免的方法在于从头部Add,但这样的最终结果为倒序
      LinkedList<int> lList = new LinkedList<int>();//原链表
      LinkedList<int> lList2 = new LinkedList<int>();//保存结果的链表
      lList.Add(1);
      lList.Add(2);
      lList.Add(1);
      lList.Add(3);
      lList.Add(3);
      lList.Add(4);
      lList.Add(5);
      Node<int> m = new Node<int>();
      m = lList.Head;
      while (m != null)
      {
        Console.WriteLine(m.Data);
        m = m.Next;
      }
      Console.ReadLine();
      Node<int> node1 = lList.Head;//标识原链表的当前要参与比较大小的元素,即可能放入链表2中的元素
      Node<int> node2;//标识结果单链表的最后一个元素,避免使用Add函数造成多次遍历
      Node<int> node3;//对node1的后继进行暂时保存,并最终付给node1
      node3 = node1.Next;
      lList2.Head = node1;//链表1的头结点肯定要加入链表2
      node2 = lList2.Head;//node2表示链表2的最后一个元素,此时最后一个元素为头结点
      node2.Next = null;//由于是把node1赋给了链表2的头结点,必须把它的后续结点设为null,否则会一起带过来
      node1 = node3;//如果没有node3暂存node1的后继,对lList2.Head后继赋为null的就会同时修改node1的后继,因为两者指向同一块内存
      while (node1 != null)
      {
        //在iList2中比较大小
        Node<int> tmp = lList2.Head;
        if (node1.Data.Equals(tmp.Data))
        {
          node1 = node1.Next;
          continue;//若相等,则node1向后移一位,重新计算
        }
        else
        {
          Node<int> tmp2 = tmp;
          tmp = tmp.Next;//tmp标识链表2的用于循环的节点,与node比较
          if (tmp == null)//当链表2中现有元素与node1都不相等时
          {
            node3 = node1.Next;
            node2.Next = node1;
            node2 = node1;
            node2.Next = null;
            node1 = node3;
            continue;
          }
          while (tmp != null)//tmp不为null时,一直循环到它为null
          {
            if (node1.Data.Equals(tmp.Data))
            {
              node1 = node1.Next;
            }
            else
            {
              tmp2 = tmp;
              tmp = tmp.Next;
              if (tmp == null)
              {
                node3 = node1.Next;
                node2.Next = node1;
                node2 = node1;
                node2.Next = null;
                node1 = node3;
              }
            }
          }
        }
      }
      //输出清除重复处理后的数组
      Node<int> n = new Node<int>();
      n = lList2.Head;
      while (n!= null)
      {
        Console.WriteLine(n.Data);
        n = n.Next;
      }
      Console.ReadLine();
      */
    }
  }
}

希望本文所述对大家C#程序设计有所帮助。