数据结构之链表篇——C#语言实现_动态链表





在数据结构的学习中,动态链表是动态数据结构的最基本的形式,也是最常见的一种线性数据结构,使用范围广。



在创建动态链表时,对不同节点之间的链接关系梳理清楚,这一点十分重要。。。



比如,下面开始试着用C#编程语言来实现基本的链表操作,包括:


  1. 在动态链表中插入节点
  2. 在动态链表中删除节点
  3. 求动态链表的长度
  4. 打印输出动态链表的全部元素 





动态链表的节点,可以按如下方式定义:




数据结构之链表篇——C#语言实现_删除节点_02






























// 作者:文方俊
// 日期:2020年11月18日
    // 功能: 定义动态链表结构
class Node
{
public Node(int value)
{
this.value = value;
this.next = null;
}
public void setValue(int val)
{
this.value = val;
}
public int getValue()
{
return this.value;
}
private int value;
public Node next;
}





动态链表中插入节点,



























    // 作者:文方俊
// 日期:2020年11月18日
    // 功能: 在动态链表中插入新节点
public void insertNode(int value)
{
Node newNode = new Node(value);
newNode.setValue(value);
//头节点为空
if (null == this.head)
{
this.head = newNode;
}
else
//头节点不为空
{
newNode.next = this.head;
this.head = newNode;
}
}





动态链表中删除节点,


















































    // 作者:文方俊
// 日期:2020年11月18日
// 功能:在动态链表中删除节点
public Node deleteNode(int value)
{
Node deleteNode = null;
// 头节点为空
if (null == this.head)
{
return null;
}
else
{
// 头节点不为空
// 删除头节点
if (value == this.head.getValue())
{
deleteNode = this.head;
this.head = this.head.next;
}
// 删除节点
else
{
Node preNode = this.head;
while ((null != preNode.next) && (value != preNode.next.getValue()))
{
preNode = preNode.next;
}
// 删除节点不在链表中
if (null == preNode.next)
{
return null;
}
if (value == preNode.next.getValue())
{
deleteNode = preNode.next;
preNode.next = preNode.next.next;
}
}
}
return deleteNode;
}






打印输出动态链表,








































    // 作者:文方俊
// 日期:2020年11月18日
// 功能:打印输出动态链表
public List<int> printLinkedList()
{
Node tmp = this.head;
List<int> values = new List<int>(0);
while (null != tmp)
{
values.Add(tmp.getValue());
tmp = tmp.next;
}
return values;
}
public void printValues(List<int> values)
{
// 输出链表元素
Console.Write("打印输出链表元素:");
for (int i = 0; i < values.Count(); i++)
{
if (i < values.Count() - 1)
{
Console.Write(values[i] + "->");
}
else
{
Console.Write(values[i]);
}
}
Console.WriteLine();
}





计算动态链表的长度,























    // 作者:文方俊
// 日期:2020年11月18日
// 功能:输出动态链表的长度
public int lengthLinkedList()
{
Node tmp = this.head;
int len_list = 0;
while (null != tmp)
{
len_list++;
tmp = tmp.next;
}
return len_list;
}






动态链表的完整代码实现,可以参考下面的C#程序,































































































    // 作者:文方俊
// 日期:2020年11月18日
// 功能:动态链表中 LinkedList.cs, Node.cs的实现


class LinkedList
{
public LinkedList()
{
this.head = null;
}
public void insertNode(int value)
{
Node newNode = new Node(value);
newNode.setValue(value);
//头节点为空
if (null == this.head)
{
this.head = newNode;
}
else
//头节点不为空
{
newNode.next = this.head;
this.head = newNode;
}
}


public Node deleteNode(int value)
{
Node deleteNode = null;
// 头节点为空
if (null == this.head)
{
return null;
}
else
{
// 头节点不为空
// 删除头节点
if (value == this.head.getValue())
{
deleteNode = this.head;
this.head = this.head.next;
}
// 删除节点
else
{
Node preNode = this.head;
while ((null != preNode.next) && (value != preNode.next.getValue()))
{
preNode = preNode.next;
}
// 删除节点不在链表中
if (null == preNode.next)
{
return null;
}
if (value == preNode.next.getValue())
{
deleteNode = preNode.next;
preNode.next = preNode.next.next;
}
}
}
return deleteNode;
}


public int lengthLinkedList()
{
Node tmp = this.head;
int len_list = 0;
while (null != tmp)
{
len_list++;
tmp = tmp.next;
}
return len_list;
}
public List<int> printLinkedList()
{
Node tmp = this.head;
List<int> values = new List<int>(0);
while (null != tmp)
{
values.Add(tmp.getValue());
tmp = tmp.next;
}
return values;
}
public void printValues(List<int> values)
{
// 输出链表元素
Console.Write("打印输出链表元素:");
for (int i = 0; i < values.Count(); i++)
{
if (i < values.Count() - 1)
{
Console.Write(values[i] + "->");
}
else
{
Console.Write(values[i]);
}
}
Console.WriteLine();
}
private Node head;
}








// 作者:文方俊
// 日期:2020年11月18日
class Node
{
public Node(int value)
{
this.value = value;
this.next = null;
}
public void setValue(int val)
{
this.value = val;
}
public int getValue()
{
return this.value;
}
private int value;
public Node next;
}





动态链表的C#测试代码, 











































    // 作者:文方俊
// 日期:2020年11月18日
    // 功能:动态链表的测试程序
    
    
class Program
{
static void Main(string[] args)
{
LinkedList list = new LinkedList();

// 插入链表元素
list.insertNode(1);
list.insertNode(2);
list.insertNode(3);
list.insertNode(4);
list.insertNode(5);


for (int i=10; i<20; i++)
{
list.insertNode(i);
}


// 打印输出链表元素
Console.Write("打印输出链表元素:");
List<int> values = list.printLinkedList();
list.printValues(values);


// 打印链表的长度
Console.WriteLine("链表的当前长度为:" + list.lengthLinkedList());


// 删除节点
Console.WriteLine("删除节点1");
list.deleteNode(1);
Console.WriteLine("删除节点10");
list.deleteNode(10);
Console.WriteLine("删除节点19");
list.deleteNode(19);


// 打印输出链表元素
Console.Write("打印输出链表元素:");
List<int> output_values = list.printLinkedList();
list.printValues(output_values);


// 打印链表的长度
Console.WriteLine("链表的当前长度为:" + list.lengthLinkedList());


Console.ReadKey();
}
}





附录: 包含测试程序的完成动态链表实现


































    // 作者:文方俊
// 日期:2020年11月18日
    // 功能:动态链表的完整实现程序




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace LinkedListCSharp
{
class Program
{
static void Main(string[] args)
{
LinkedList list = new LinkedList();

// 插入链表元素
list.insertNode(1);
list.insertNode(2);
list.insertNode(3);
list.insertNode(4);
list.insertNode(5);


for (int i=10; i<20; i++)
{
list.insertNode(i);
}


// 打印输出链表元素
Console.Write("打印输出链表元素:");
List<int> values = list.printLinkedList();
list.printValues(values);


// 打印链表的长度
Console.WriteLine("链表的当前长度为:" + list.lengthLinkedList());


// 删除节点
Console.WriteLine("删除节点1");
list.deleteNode(1);
Console.WriteLine("删除节点10");
list.deleteNode(10);
Console.WriteLine("删除节点19");
list.deleteNode(19);


// 打印输出链表元素
Console.Write("打印输出链表元素:");
List<int> output_values = list.printLinkedList();
list.printValues(output_values);


// 打印链表的长度
Console.WriteLine("链表的当前长度为:" + list.lengthLinkedList());


Console.ReadKey();
}
}




class LinkedList
{
public LinkedList()
{
this.head = null;
}
public void insertNode(int value)
{
Node newNode = new Node(value);
newNode.setValue(value);
//头节点为空
if (null == this.head)
{
this.head = newNode;
}
else
//头节点不为空
{
newNode.next = this.head;
this.head = newNode;
}
}


public Node deleteNode(int value)
{
Node deleteNode = null;
// 头节点为空
if (null == this.head)
{
return null;
}
else
{
// 头节点不为空
// 删除头节点
if (value == this.head.getValue())
{
deleteNode = this.head;
this.head = this.head.next;
}
// 删除节点
else
{
Node preNode = this.head;
while ((null != preNode.next) && (value != preNode.next.getValue()))
{
preNode = preNode.next;
}
// 删除节点不在链表中
if (null == preNode.next)
{
return null;
}
if (value == preNode.next.getValue())
{
deleteNode = preNode.next;
preNode.next = preNode.next.next;
}
}
}
return deleteNode;
}


public int lengthLinkedList()
{
Node tmp = this.head;
int len_list = 0;
while (null != tmp)
{
len_list++;
tmp = tmp.next;
}
return len_list;
}
public List<int> printLinkedList()
{
Node tmp = this.head;
List<int> values = new List<int>(0);
while (null != tmp)
{
values.Add(tmp.getValue());
tmp = tmp.next;
}
return values;
}
public void printValues(List<int> values)
{
// 输出链表元素
Console.Write("打印输出链表元素:");
for (int i = 0; i < values.Count(); i++)
{
if (i < values.Count() - 1)
{
Console.Write(values[i] + "->");
}
else
{
Console.Write(values[i]);
}
}
Console.WriteLine();
}
private Node head;
}








// 作者:文方俊
// 日期:2020年11月18日
class Node
{
public Node(int value)
{
this.value = value;
this.next = null;
}
public void setValue(int val)
{
this.value = val;
}
public int getValue()
{
return this.value;
}
private int value;
public Node next;
}
}









数据结构之链表篇——C#语言实现_动态链表_03




数据结构之链表篇——C#语言实现_动态链表_04