下面是一个简单的通用双链表,支持Add()索引器,ToString()其余部分留给读者练习 - 这应该给你一个想法.
public class LinkedList<T>
{
protected LinkedListNode<T> root = null;
protected LinkedListNode<T> last = null;
public LinkedList()
{
}
public string ToString()
{
StringBuilder sb = new StringBuilder();
var node = root;
while (node != null)
{
sb.Append("{ " + node.Data.ToString() + " } ");
node = node.Next;
}
return sb.ToString();
}
public T this[int index]
{
get
{
var node = GetAt(index);
if(node == null)
throw new ArgumentOutOfRangeException();
return node.Data;
}
set
{
var node = GetAt(index);
if (node == null)
throw new ArgumentOutOfRangeException();
node.Data = value;
}
}
private LinkedListNode<T> GetAt(int index)
{
var current = root;
for(int i=0;i<index;i++)
{
if (current == null)
return null;
current = current.Next;
}
return current;
}
public void Add(T data)
{
if (root == null)
{
root = new LinkedListNode<T>(data);
last = root;
}
else
{
last.Next = new LinkedListNode<T>(data);
last.Next.Previous = last;
last = last.Next;
}
}
}
public class LinkedListNode<T>
{
public T Data {get;set;}
public LinkedListNode(T data)
{
Data = data;
}
public LinkedListNode<T> Next { get; set; }
public LinkedListNode<T> Previous { get; set; }
}
Run Code Online (Sandbox Code Playgroud)