```namespace 线性表
{
public class Node<T>
{
private T data;
private Node<T> next;

public Node(T val, Node<T> p)
{
data = val;
next = p;
}

public Node(Node<T> p)
{
next = p;
}

public Node(T val)
{
data = val;
next = null;
}

public Node()
{
data = default(T);
next = null;
}

public T Data
{
get { return data; }
set { data = value; }
}

public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}```

```using System;
using System.Text;

namespace 线性表
{
{

{
set { head = value; }
}

{
}

/// <summary>
/// 类索引器
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
return this.GetItemAt(index);
}
}

/// <summary>
/// 返回单链表的长度
/// </summary>
/// <returns></returns>
public int Count()
{
int len = 0;
while (p != null)
{
len++;
p = p.Next;
}
return len;
}

/// <summary>
/// 清空
/// </summary>
public void Clear()
{
}

/// <summary>
/// 是否为空
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
}

/// <summary>
/// 在最后附加元素
/// </summary>
/// <param name="item"></param>
public void Append(T item)
{
Node<T> d = new Node<T>(item);
Node<T> n = new Node<T>();

{
return;
}

while (n.Next != null)
{
n = n.Next;
}
n.Next = d;
}

//前插
public void InsertBefore(T item, int i)
{
if (IsEmpty() || i < 0)
{
Console.WriteLine("List is empty or Position is error!");
return;
}

//在最开头插入
if (i == 0)
{
Node<T> q = new Node<T>(item);
return;
}

Node<T> d = new Node<T>();
int j = 0;

//找到位置i的前一个元素d
while (n.Next != null && j < i)
{
d = n;
n = n.Next;
j++;
}

if (n.Next == null) //说明是在最后节点插入(即追加)
{
Node<T> q = new Node<T>(item);
n.Next = q;
q.Next = null;
}
else
{
if (j == i)
{
Node<T> q = new Node<T>(item);
d.Next = q;
q.Next = n;
}
}
}

/// <summary>
/// 在位置i后插入元素item
/// </summary>
/// <param name="item"></param>
/// <param name="i"></param>
public void InsertAfter(T item, int i)
{
if (IsEmpty() || i < 0)
{
Console.WriteLine("List is empty or Position is error!");
return;
}

if (i == 0)
{
Node<T> q = new Node<T>(item);
return;
}

int j = 0;

while (p != null && j < i)
{
p = p.Next;
j++;
}
if (j == i)
{
Node<T> q = new Node<T>(item);
q.Next = p.Next;
p.Next = q;
}
else
{
Console.WriteLine("Position is error!");
}
}

/// <summary>
/// 删除位置i的元素
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public T RemoveAt(int i)
{
if (IsEmpty() || i < 0)
{
Console.WriteLine("Link is empty or Position is error!");
return default(T);
}

Node<T> q = new Node<T>();
if (i == 0)
{
return q.Data;
}

int j = 0;

while (p.Next != null && j < i)
{
j++;
q = p;
p = p.Next;
}

if (j == i)
{
q.Next = p.Next;
return p.Data;
}
else
{
Console.WriteLine("The node is not exist!");
return default(T);
}
}

/// <summary>
/// 获取指定位置的元素
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public T GetItemAt(int i)
{
if (IsEmpty())
{
Console.WriteLine("List is empty!");
return default(T);
}

Node<T> p = new Node<T>();

if (i == 0)
{
return p.Data;
}

int j = 0;

while (p.Next != null && j < i)
{
j++;
p = p.Next;
}

if (j == i)
{
return p.Data;
}
else
{
Console.WriteLine("The node is not exist!");
return default(T);
}
}

//按元素值查找索引
public int IndexOf(T value)
{
if (IsEmpty())
{
Console.WriteLine("List is Empty!");
return -1;
}
Node<T> p = new Node<T>();
int i = 0;
while (!p.Data.Equals(value) && p.Next != null)
{
p = p.Next;
i++;
}
return i;
}

/// <summary>
/// 元素反转
/// </summary>
public void Reverse()
{
t = t.Next;

while (t!=null)
{
result.InsertBefore(t.Data, 0);
t = t.Next;
}
result = null;//显式清空原链表的引用，以便让GC能直接回收
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(n.Data.ToString() + ",");
while (n.Next != null)
{
sb.Append(n.Next.Data.ToString() + ",");
n = n.Next;
}
return sb.ToString().TrimEnd(',');

}
}
}```

下面是单链表插入和删除的算法图解：

```            Console.WriteLine("-------------------------------------");
Console.WriteLine("单链表测试开始...");

1023 篇文章77 人订阅

0 条评论

## 相关文章

### C# 通过反射初探ORM框架的实现原理

以前学的Java进行开发，多用到Mybatis，Hiberante等ORM框架，最近需要上手一个C#的项目，由于不是特别难，也不想再去学习C#的ORM框架，...

248100

20850

### C# WPF获取任务栏时间区域的Rectangle

[StructLayout(LayoutKind.Sequential)] public struct WindowRect {...

21370

18880

### C#像运行一个exe 程序一样运行一个dll文件

[DllImport("kernel32.dll")] public static extern int WinExec(str...

20680

### C# WPF 让你的窗口始终钉在桌面上

IntPtr hWnd = new WindowInteropHelper(Application.Current.MainWindow...

274100

33490

25560

510100

24450