4.单链表类
namespace DSList
{
public partial class LinkList<T> : IListDS<T> where T:IComparable<T>
{
//fields
private Node<T> head;
//property
public Node<T> Head
{
get
{
return head;
}
set
{
head = value;
}
}
//constructor
public LinkList()
{
head = null;
}
//base methods
public int GetLength()
{
int len = 0;
Node<T> curNode = head;
while (curNode != null)
{
++len;
curNode = curNode.Next;
}
return len;
}
public bool IsEmpty()
{
if (head == null)
{
return true;
}
else
{
return false;
}
}
public void Clear()
{
head = null;
}
public void Append(T item)
{
Node<T> newNode = new Node<T>(item);
if (head == null)
{
head = newNode;
return;
}
Node<T> curNode = head;
while (curNode.Next != null)
{
curNode = curNode.Next;
}
curNode.Next = newNode;
}
public void Insert(int pos, T item)
{
if (IsEmpty())
{
Console.WriteLine("LinkList is empty!");
return;
}
if (pos < 0)
{
Console.WriteLine("Position is error!");
return;
}
Node<T> newNode = new Node<T>(item);
if (pos == 0)
{
newNode.Next = head;
head = newNode;
return;
}
Node<T> frontNode = null;
Node<T> curNode = head.Next;
int index = 1;
while (curNode.Next != null && index < pos)
{
frontNode = curNode;
curNode = curNode.Next;
++index;
}
if (index == pos)
{
newNode.Next = curNode;
frontNode.Next = newNode;
}
else
{
Console.WriteLine("Position is out of range!");
return;
}
}
public T Remove(int pos)
{
if (IsEmpty())
{
Console.WriteLine("LinkList is empty!");
return default(T);
}
if (pos < 0)
{
Console.WriteLine("Position is error!");
return default(T);
}
T data = default(T);
if (pos == 0)
{
data = head.Data;
head = head.Next;
return data;
}
Node<T> frontNode = null;
Node<T> curNode = head.Next;
int index = 1;
while (curNode.Next != null && index < pos)
{
++index;
frontNode = curNode;
curNode = curNode.Next;
}
if (index == pos)
{
frontNode.Next = curNode.Next;
return curNode.Data;
}
else
{
Console.WriteLine("Position is out of range!");
return default(T);
}
}
public void Delete(T item)
{
int pos = this.Locate(item);
this.Remove(pos);
}
public T GetElem(int pos)
{
if (IsEmpty())
{
Console.WriteLine("LinkList is empty!");
return default(T);
}
Node<T> curNode = head;
int index = 0;
while (curNode.Next != null && index < pos)
{
++index;
curNode = curNode.Next;
}
if (index == pos)
{
return curNode.Data;
}
else
{
Console.WriteLine("Position is out of range!");
return default(T);
}
}
public int Locate(T item)
{
if (IsEmpty())
{
Console.WriteLine("LinkList is empty!");
return -1;
}
Node<T> curNode = head;
int index = 0;
while (!curNode.Data.Equals(item)&& curNode.Next!=null)
{
curNode = curNode.Next;
++index;
}
return index;
}
}
public partial class LinkList<T> : IListDS<T>
{
//other methods
public void InsertPost(int pos, T item)
{
if (IsEmpty())
{
Console.WriteLine("LinkList is empty!");
return;
}
if (pos < 0)
{
Console.WriteLine("Position is error!");
return;
}
Node<T> newNode = new Node<T>(item);
if (pos == 0)
{
newNode.Next = head.Next;
head.Next = newNode;
return;
}
Node<T> curNode = head.Next;
Node<T> nextNode = curNode.Next;
int index = 1;
while (nextNode != null && index < pos)
{
++index;
curNode = nextNode;
nextNode = nextNode.Next;
}
if (index == pos)
{
newNode.Next = nextNode;
curNode.Next = newNode;
}
else
{
Console.WriteLine("Position is out of range!");
return;
}
}
public void Reverse()
{
if (IsEmpty())
{
Console.WriteLine("SingleLinkList is empty!");
return;
}
Node<T> curNode = Head;
Node<T> tmpNode = null;
Head = null;
while (curNode != null)
{
tmpNode = curNode;
curNode = curNode.Next;
tmpNode.Next = Head;
Head = tmpNode;
}
}
public void Merge(LinkList<T> Sla, LinkList<T> Slb)
{
Node<T> AcurNode = Sla.Head;
Node<T> BcurNode = Slb.Head;
Node<T> tmpNode = new Node<T>();
while (AcurNode != null && BcurNode != null)
{
if (AcurNode.Data.CompareTo(BcurNode.Data) < 0)
{
tmpNode = AcurNode;
AcurNode = AcurNode.Next;
}
else
{
tmpNode = BcurNode;
BcurNode = BcurNode.Next;
}
this.Append(tmpNode.Data);
}
if (AcurNode == null)
{
AcurNode = BcurNode;
}
while (AcurNode != null)
{
tmpNode = AcurNode;
AcurNode = AcurNode.Next;
this.Append(tmpNode.Data);
}
}
public void Purge(LinkList<T> Ha)
{
Node<T> nextNode = Ha.Head;
Node<T> tmpNode = null;
Node<T> curNode = null;
curNode = nextNode;
nextNode = nextNode.Next;
curNode.Next = null;
this.Head = curNode;
while (nextNode != null)
{
curNode = nextNode;
nextNode = nextNode.Next;
tmpNode = this.Head;
while (tmpNode != null && !tmpNode.Data.Equals(curNode.Data))
{
tmpNode = tmpNode.Next;
}
if (tmpNode == null)
{
curNode.Next = this.Head;
this.Head = curNode;
}
}
}
public void Sort()
{
Node<T> curNode = this.head;
Node<T> nextNode = this.head.Next;
T temp = default(T);
while (curNode != null)
{
while (nextNode != null)
{
if (curNode.Data.CompareTo(nextNode.Data) > 0)
{
temp = nextNode.Data;
nextNode.Data = curNode.Data;
curNode.Data = temp;
}
nextNode = nextNode.Next;
}
nextNode = curNode.Next;
curNode = curNode.Next;
}
}
public void Output()
{
for(int i=0;i<this.GetLength();i++)
{
Console.Write(this.GetElem(i) + " ");
}
Console.WriteLine();
}
}
}