interface IlistDS<T>
{
int GetLength();
void Clear();
bool IsEmpty();
void Add(T item);
void Insert(T item, int index);
T Delete(int index);
T this[int index] { get; }
T GetEle(int index);
int Locat(T item);
}
/// <summary>
/// 单链表节点
/// </summary>
/// <typeparam name="T"></typeparam>
class Node<T>
{
private T data;//存储数据
private Node<T> next;//指针 用来指向下一个元素
public Node()
{
this.data = default(T);
this.next = null;
}
public Node(T Value)
{
this.data = Value;
this.next = null;
}
public Node(T Value,Node<T> next)
{
this.data = Value;
this.next = next;
}
public Node(Node<T> next)
{
this.next = next;
}
public T Data
{
get { return this.data; }
set { this.data = value; }
}
public Node<T> Next
{
get { return this.next; }
set { this.next = value; }
}
}
class LinkList<T> : IlistDS<T>
{
private Node<T> head;//存储一个头结点
public LinkList()
{
this.head = null;
}
public T this[int index]
{
get
{
Node<T> Temp = this.head;
for (int i = 1; i <= index; i++)
{
Temp = Temp.Next;
}
return Temp.Data;
}
}
public void Add(T item)
{
Node<T> newNode = new Node<T>(item);//根据新的数据创建一个新的节点
//如果头节点为空,那么这个新的节点就是头节点
if (this.head == null)
{
this.head = newNode;
}
else//把新节点放到链表的尾部
{
//要访问到链表的尾节点
Node<T> Temp = this.head;
while (true)
{
if (Temp.Next != null)
{
Temp = Temp.Next;
}
else
{
break;
}
}
Temp.Next = newNode;//把新节点放到链表的尾部
}
}
public void Clear()
{
this.head = null;
}
public T Delete(int index)
{
T data = default(T);
if (index == 0)//删除头节点
{
data = head.Data;
this.head = head.Next;
}
else
{
Node<T> Temp = this.head;
for (int i = 1; i <= index - 1; i++)
{
Temp = Temp.Next;
}
data = Temp.Next.Data;
Temp.Next = Temp.Next.Next;
}
return data;
}
public T GetEle(int index)
{
return this[index];
}
public int GetLength()
{
if (this.head == null) return 0;
int Length = 1;
Node<T> Temp = this.head;
while (true)
{
if (Temp.Next != null)
{
Temp = Temp.Next;
Length++;
}
else
{
break;
}
}
return Length;
}
public void Insert(T item, int index)
{
Node<T> newNode = new Node<T>(item);
if (index == 0)
{
newNode.Next = head;
head = newNode;
}
else
{
Node<T> Temp = this.head;
for (int i = 1; i <= index - 1; i++)
{
Temp = Temp.Next;
}
newNode.Next = Temp.Next;
Temp.Next = newNode;
}
}
public bool IsEmpty()
{
return head == null;
}
public int Locat(T item)
{
Node<T> temp = head;
int index = 0;
if (temp == null)
{
return -1;
}
else
{
while (true)
{
if (temp.Data.Equals(item))
{
return index;
}
else
{
if (temp.Next != null)
{
temp = temp.Next;
index++;
}
else
{
break;
}
}
}
return -1;
}
}
}
static void Main(string[] args)
{
//使用自己的顺序表
LinkList<string> seqList = new LinkList<string>();
seqList.Add("123");
seqList.Add("456");
seqList.Add("789");
Console.WriteLine(seqList.GetEle(0));
Console.WriteLine(seqList[0]);
seqList.Insert("777", 1);
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.Write(seqList[i] + " ");
}
Console.WriteLine();
seqList.Delete(0);
for (int i = 0; i < seqList.GetLength(); i++)
{
Console.Write(seqList[i] + " ");
}
Console.WriteLine();
seqList.Clear();
Console.WriteLine(seqList.GetLength());
Console.ReadKey();
}