6.双链表类
namespace DSList
{
public partial class DbLinkList<T> : IListDS<T> where T : IComparable<T>
{
private DbNode<T> head; //Head reference domain
//Head property
public DbNode<T> Head
{
get
{
return head;
}
set
{
head = value;
}
}
//Constructor
public DbLinkList()
{
head = null;
}
//Base Methods
public int GetLength()
{
DbNode<T> curNode = head;
int len = 0;
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)
{
DbNode<T> newNode = new DbNode<T>(item);
if (head == null)
{
this.head = newNode;
return;
}
DbNode<T> curNode = head;
while (curNode.Next != null)
{
curNode = curNode.Next;
}
curNode.Next = newNode;
newNode.Prev = curNode;
}
public void Insert(int pos, T item)
{
if (IsEmpty())
{
Console.WriteLine("DbLinkList is empty!");
return;
}
if (pos < 0)
{
Console.WriteLine("Position is error!");
return;
}
DbNode<T> newNode = new DbNode<T>(item);
if (pos == 0)
{
newNode.Next = head;
head.Prev = newNode;
head = newNode;
return;
}
DbNode<T> frontNode = null;
DbNode<T> curNode = head.Next;
int index = 1;
while (curNode.Next != null&&index<pos)
{
++index;
frontNode = curNode;
curNode = curNode.Next;
}
if (index == pos)
{
frontNode.Next = newNode;
newNode.Prev = frontNode;
newNode.Next = curNode;
curNode.Prev = newNode;
}
else
{
Console.WriteLine("Position is out of the range!");
return;
}
}
public T Remove(int pos)
{
if (IsEmpty())
{
Console.WriteLine("DbLinkList 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;
}
DbNode<T> frontNode = null;
DbNode<T> curNode = head.Next;
int index = 1;
while (curNode.Next != null && index < pos)
{
++index;
frontNode = curNode;
curNode = curNode.Next;
}
if (index == pos && curNode.Next != null)
{
frontNode.Next = curNode.Next;
curNode.Next.Prev = frontNode;
return curNode.Data;
}
if (index == pos && curNode.Next == null)
{
frontNode.Next = curNode.Next;
return curNode.Data;
}
else
{
Console.WriteLine("Position is out of the 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("DbLinkList is empty!");
return default(T);
}
if (pos < 0)
{
Console.WriteLine("Position is error!");
return default(T);
}
DbNode<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 the range!");
return default(T);
}
}
public int Locate(T item)
{
if (IsEmpty())
{
Console.WriteLine("DbLinkList is empty or Position is error!");
return 0;
}
DbNode<T> newNode = new DbNode<T>(item);
DbNode<T> curNode = head;
int index=0;
while (curNode.Next != null)
{
if (curNode.Data.Equals(newNode.Data))
{
break;
}
curNode = curNode.Next;
++index;
}
return index;
}
}
public partial class DbLinkList<T> : IListDS<T> where T : IComparable<T>
{
//Others methods
public void InsertPost(int pos, T item)
{
if (IsEmpty())
{
Console.WriteLine("DbLinkList is empty!");
return;
}
if (pos < 0)
{
Console.WriteLine("Position is error!");
return;
}
DbNode<T> newNode = new DbNode<T>(item);
if (pos == 0)
{
newNode.Next = head.Next;
head.Next.Prev = newNode;
head.Next = newNode;
newNode.Prev = head;
return;
}
DbNode<T> curNode = head.Next;
DbNode<T> NextNode = curNode.Next;
int index = 1;
while (NextNode != null && index < pos)
{
++index;
curNode = NextNode;
NextNode = NextNode.Next;
}
if (index == pos && NextNode != null)
{
newNode.Next = NextNode;
NextNode.Prev = newNode;
curNode.Next = newNode;
newNode.Prev = curNode;
}
if (index == pos && NextNode == null)
{
newNode.Prev = curNode;
curNode.Next = newNode;
}
else
{
Console.WriteLine("Position is out of the range!");
return;
}
}
public void Reverse()
{
if (IsEmpty())
{
Console.WriteLine("SLinkList is empty!");
return;
}
DbNode<T> nextNode = Head;
DbNode<T> tmpNode = null;
Head = null;
while (nextNode != null)
{
tmpNode = nextNode;
nextNode = nextNode.Next;
tmpNode.Next = Head;
Head = tmpNode;
head.Prev = tmpNode;
}
}
public void Merge(DbLinkList<T> la, DbLinkList<T> lb)
{
DbNode<T> AcurNode = la.Head;
DbNode<T> BcurNode = lb.Head;
DbNode<T> tmpNode = null;
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(DbLinkList<T> la)
{
DbNode<T> nextNode = la.Head;
DbNode<T> curNode=null;
DbNode<T> tmpNode=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;
this.Head.Next.Prev = head;
}
}
}
public void Sort()
{
DbNode<T> curNode = this.Head;
DbNode<T> nextNode = this.head.Next;
T tmpData = default(T);
while (curNode != null)
{
while (nextNode != null)
{
if (curNode.Data.CompareTo(nextNode.Data) > 0)
{
tmpData = nextNode.Data;
nextNode.Data = curNode.Data;
curNode.Data = tmpData;
}
nextNode = nextNode.Next;
}
nextNode = curNode.Next;
curNode = curNode.Next;
}
}
public void Output()
{
for (int i = 0; i < GetLength(); i++)
{
Console.Write( GetElem(i) + " ");
}
Console.WriteLine();
}
}
}