+关注继续查看

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("单链表测试开始...");

51 0
Java Collection笔记之ArrayList
<div class="markdown_views"> <h1 id="1前言">1、前言</h1> <p>ArrayList 是一个数组队列，相当于动态数组。与Java中的数组相比，它的容量能动态增长。它继承于AbstractList，实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。</p> <p><
1460 0
HBase数据结构（读书笔记 ）

1204 0

491 0

31 0
WCF 笔记 (2) - 传输泛型 List 对象

667 0
history命令解析_学习笔记

799 0
AndroidStudio笔记（3）提升效率的 Live Templates

1197 0
+关注

1102

0

JS零基础入门教程（上册）