那天去面试,用C#实现单链表,当时没有写出来,只好回来弄明白怎么回事了。
//-----------------------------------------------------------
public interface ISingleLink<T>
{
/// <summary>
/// 添加节点
/// </summary>
void AddNode();
/// <summary>
/// 添加带值的节点
/// </summary>
/// <param name="t"></param>
void AddNode(T t);
/// <summary>
/// 插入到某个位置的节点
/// </summary>
/// <param name="position">插入位置</param>
void InsertNode(int position);
/// <summary>
/// 插入到某个位置的节点
/// </summary>
/// <param name="t">节点值</param>
/// <param name="position">插入位置</param>
void InsertNode(int position, T t);
/// <summary>
/// 插入到头节点
/// </summary>
void InsertHead();
/// <summary>
/// 插入到头节点
/// </summary>
/// <param name="t">节点值</param>
void InsertHead(T t);
/// <summary>
/// 获取某个位置的节点值
/// </summary>
/// <param name="position">节点位置</param>
T GetNode(int position);
/// <summary>
/// 设置某节点值
/// </summary>
/// <param name="t">节点值</param>
/// <param name="position">节点位置</param>
void SetNodeValue(int position, T t);
/// <summary>
/// 删除某个位置的节点
/// </summary>
/// <param name="position">节点位置</param>
void DeleteNode(int position);
/// <summary>
/// 删除所有节点
/// </summary>
void DeleteAllNode();
/// <summary>
/// 删除最后一个节点
/// </summary>
void DeleteLastNode();
/// <summary>
/// 是否为空链表
/// </summary>
/// <returns></returns>
bool IsEmpty { get;}
/// <summary>
/// 链表长度
/// </summary>
/// <returns></returns>
int LinkLength { get;}
}
//-----------------------------------------------------------
public class Node<T>
{
private T nodeValue;
private Node<T> nextNode;
public T Value
{
get
{
return nodeValue;
}
set
{
nodeValue = value;
}
}
public Node<T> NextNode
{
get
{
return nextNode;
}
set
{
nextNode = value;
}
}
public Node()
{
nextNode = null;
}
public Node(T t)
: this()
{
nodeValue = t;
}
}
public class SingleLinks<T> : ISingleLink<T>
{
private int count;
private Node<T> headNode;
public SingleLinks()
{
count = 0;
headNode = new Node<T>();
}
public SingleLinks(T t)
:this()
{
count = 1;
headNode.NextNode = new Node<T>(t);
}
public void AddNode()
{
InsertNodeImplement(count, default(T));
}
public void AddNode(T t)
{
InsertNodeImplement(count, t);
}
public void InsertNode(int position)
{
InsertNodeImplement(position - 1, default(T));
}
public void InsertNode(int position, T t)
{
InsertNodeImplement(position - 1, t);
}
private void InsertNodeImplement(int position, T t)
{
checkIndex(position);
Node<T> currentNode = headNode;
while(position-- > 0)
{
currentNode = currentNode.NextNode;
}
Node<T> addNode = new Node<T>(t);
addNode.NextNode = currentNode.NextNode;
currentNode.NextNode = addNode;
++count;
}
public void InsertHead()
{
InsertNodeImplement(0, default(T));
}
public void InsertHead(T t)
{
InsertNodeImplement(0, t);
}
public T GetNode(int position)
{
checkIndex(position);
Node<T> currentNode = headNode;
while (--position > 0)
{
currentNode = currentNode.NextNode;
}
return currentNode.NextNode.Value;
}
public void SetNodeValue(int position, T t)
{
checkIndex(position);
Node<T> currentNode = headNode;
while (--position > 0)
{
currentNode = currentNode.NextNode;
}
currentNode.NextNode.Value = t;
}
public void DeleteAllNode()
{
headNode.NextNode = null;
count = 0;
}
public void DeleteNode(int position)
{
checkIndex(position - 1);
Node<T> currentNode = headNode;
while (--position > 0)
{
currentNode = currentNode.NextNode;
}
currentNode.NextNode = currentNode.NextNode.NextNode;
--count;
}
public void DeleteLastNode()
{
DeleteNode(count);
}
public bool IsEmpty
{
get { return headNode.NextNode == null; }
}
public int LinkLength
{
get { return count; }
}
private void checkIndex(int position)
{
if (position > count || position < 0)
{
throw new IndexOutOfRangeException();
}
}
}