public class Node
{public object Element;
public Node Link;
public Node()
{
Element = null;
Link = null;
}
public Node(object theElement)
{
Element = theElement;
Link = null;
}
}
public class LinkedList
{
protected Node current;
protected Node header;
private int count;
//长度
public int Count
{
get
{
return count;
}
}
//构造循环链表
public LinkedList()
{
count = 0;
header = new Node("header");
header.Link = header;
}
//检验链表是否为空
public bool IsEmpty()
{
return (header.Link == null);
}
//清空链表
public void MakeEmpty()
{
header.Link = null;
}
//输出链表中的元素
public void PrintList()
{
Node current = new Node();
current = header;
while (current.Link != header)
{
Console.WriteLine(current.Link.Element);
current = current.Link;
}
}
//查找item如果存在就是返回就是和obj数据相同的的前一个节点(这样就能让链接跳过obj那数据到下一个就相当于删除了),从header开始查找,如果不存在返回的是null
private Node FindPrevious(object obj)
{
Node current = header;
while (!(current.Link == null) && current.Link.Element != obj)
current = current.Link;
return current;
}
//查找item如果存在就是返回就是和item数据相同的,从header开始查找,如果不存在返回的是null
private Node Find(object obj)
{
Node current = new Node();
current = header;
while (current.Element != obj)
current = current.Link;
return current;
}
//移除
public void Remove(object obj)
{
Node p = FindPrevious(obj);
if (!(p.Link == null))
p.Link = p.Link.Link;
count--;
}
//插入
public void Insert(object n1, object n2)
{
Node current = new Node();
Node newnode = new Node(n1);
current = Find(n2);
newnode.Link = current.Link;
current.Link = newnode;
count++;
}
//在头位置插入
public void InsertFirst(object obj)
{
Node current = new Node(obj);
current.Link = header;
header.Link = current;
count++;
}
//返回第n个节点,从后算起的第n个,起始为0
public Node Move(int n)
{
Node current = header.Link;
Node temp;
for (int i = 0; i <= n; i++)
current = current.Link;
if (current.Link == header)
current = current.Link;
temp = current;
return temp;
}
//得到头节点
public Node getFirst()
{
return header;
}