C#实现的简单链表类实例

时间:2022-06-30 12:02:16

本文实例讲述了C#实现的简单链表类。分享给大家供大家参考。具体如下:

一、关于C#链表

C#链表可用类LinkedList来存放。本文中的类MyLinkedList只是实现了该类的最基本的功能。C#中没有指针,但因为C#中类在赋值时传递的是地址,因此仍然可以利用这点制作一个链表。

二、结点类Node和链表类MyLinkedList代码

?
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
/// <summary>
/// 链表结点
/// </summary>
class Node
{
  //结点数据,前后结点
  public object Data;
  public Node PreviousNode;
  public Node NextNode;
  //构造函数
  public Node(object data = null)
  {
    Data = data;
    PreviousNode = null;
    NextNode = null;
  }
  //输出结点信息
  public override string ToString()
  {
    return Data.ToString();
  }
}
/// <summary>
/// 链表类
/// </summary>
class MyLinkedList
{
  //首结点、尾结点
  public Node First;
  public Node Last;
  //下一个结点、上一个结点
  public Node NextNode(Node n) { return n.NextNode; }
  public Node PreviousNode(Node n) { return n.PreviousNode; }
  //结点总数
  public int Count;
  //构造函数
  public MyLinkedList()
  {
    this.First = null;
    this.Last = null;
    Count = 0;
  }
  /// <summary>
  /// 在结点node1之后增加结点node2,如果没有该结点则在最后增加
  /// </summary>
  /// <param name="node1">结点1</param>
  /// <param name="node2">结点2</param>
  public void AddAfter(Node node1, Node node2)
  {
    //链表为空的情况
    if (First == null)
    {
      Console.WriteLine("Linked-list is null! Can not find node1(" + node1 + ")");
      return;
    }
    Node temp = First;
    do
    {
      if (temp.Data.Equals(node1.Data))
      {
        //如果node1是尾结点
        if (node1.NextNode == null)
        {
          node2.NextNode = null;
          node2.PreviousNode = node1;
          node1.NextNode = node2;
        }
        else //如果node1不是尾结点
        {
          node2.NextNode = node1.NextNode;
          node2.PreviousNode = node1;
          node2.NextNode.PreviousNode = node2;
          node1.NextNode = node2; ;
        }
        Count++;
        Console.WriteLine("Node(" + node2 + "): Add Complete!");
        return;
      }
      temp = temp.NextNode;
    }
    while (temp != null);
    Console.WriteLine("Can not find node(" + node1 + "), Misson defeat");
  }
  /// <summary>
  /// 在链表尾部增加结点
  /// </summary>
  public void AddLast(Node node)
  {
    //链表为空的情况
    if (this.First == null)
    {
      node.NextNode = null;
      node.PreviousNode = null;
      this.First = node;
      this.Last = node;
    }
    else //链表不为空的情况
    {
      Node temp = First;
      while(temp.NextNode != null)
      {
        temp = temp.NextNode;
      }
      temp.NextNode = node;
      node.PreviousNode = temp;
      Last = node;
    }
    Count++;
    Console.WriteLine("Node(" + node + "): Add Complete!");
  }
  /// <summary>
  /// 删除指定结点
  /// </summary>
  /// <param name="node">被删除结点</param>
  public void Delete(Node node)
  {
    if (Count == 0)
    {
      Console.WriteLine("Can not find node(" + node + ")");
      return;
    }
    Node temp = First;
    do
    {
      //如果数据部分匹配,则删去该结点
      if (temp.Data.Equals(node.Data))
      {
        //temp是尾结点
        if (temp.NextNode == null)
        {
          temp.PreviousNode.NextNode = null;
          temp = null;
        }
        else //temp不是尾结点
        {
          temp.PreviousNode.NextNode = temp.NextNode;
          temp.NextNode.PreviousNode = temp.PreviousNode;
          temp = null;
        }
        Count--;
        Console.WriteLine("Node(" + node + "): Delete Complete!");
        return;
      }
      temp = temp.NextNode;
    }
    while (temp != null);
    Console.WriteLine("Can not find node(" + node + "), Misson defeat");
  }
  /// <summary>
  /// 修改结点值
  /// </summary>
  /// <param name="node">被修改结点</param>
  /// <param name="value">结点值</param>
  public void Modify(Node node, object value)
  {
    if (Count == 0)
    {
      Console.WriteLine("Can not find node(" + node + ")");
      return;
    }
    Node temp = First;
    do
    {
      if (temp.Data.Equals(node.Data))
      {
        Console.WriteLine("Node: " + temp.Data + " → " + value.ToString());
        temp.Data = value;
        return;
      }
      temp = temp.NextNode;
    }
    while (temp != null);
  }
  /// <summary>
  /// 打印链表
  /// </summary>
  public void Print()
  {
    if (First == null)
    {
      Console.WriteLine("No nodes in this linked-list.");
      return;
    }
    else
    {
      Console.WriteLine("Print the linked-list...");
      Node temp = First;
      do
      {
        Console.WriteLine(temp.ToString());
        temp = temp.NextNode;
      }
      while (temp != null);
      Console.WriteLine("Mission Complete!");
    }
  }
}

三、Main函数的调用示例

?
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
static void Main(string[] args)
{
  MyLinkedList ll = new MyLinkedList();
  //添加三个结点 1 2(在1后) 3(在2后)
  Node n1 = new Node("node1");     
  Node n2 = new Node("node2");
  Node n3 = new Node("node3");
  ll.AddLast(n1);
  ll.AddLast(n2);
  ll.AddLast(n3);
  //添加三个结点 1.5(在1后) 2.5(在2后) 3.5(在3后)
  Node n1dot5 = new Node("node1dot5");
  Node n2dot5 = new Node("node2dot5");
  Node n3dot5 = new Node("node3dot5");
  ll.AddAfter(n1, n1dot5);
  ll.AddAfter(n2, n2dot5);
  ll.AddAfter(n3, n3dot5);
  Console.WriteLine("========================");
  //打印链表
  ll.Print();
  Console.WriteLine("========================");
  //删除结点 2 和 3,将结点 2.5 的值改为 "ThisNodeIsModified!"
  ll.Delete(n2);
  ll.Delete(n3);
  ll.Modify(n2dot5, "ThisNodeIsModified!");
  Console.WriteLine("========================");
  //打印链表
  ll.Print();
  Console.ReadLine();
}

四、运行结果

C#实现的简单链表类实例

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