www.pudn.com > MyDM1.rar > List.cs
using System;
namespace MyDM1
{
///
/// 链表类
///
//定义结点之后,开始类线性表的操作编程了.在LIST 类中,采用了,Head ,Tail, Current,三个指针,使用Append ,MoveFrist,MovePrevious,MoveNext,MoveLast ,Delete,InsertAscending,InsertUnAscending ,Clear 实现移动,添加,删除,升序插入,降序插入,清空链表操作,GetCurrentValue() 方法取得当前的值。
public class List
{
public List()
{
ListCountValue=0;
Head=null;
Tail=null;
}
private ListNode Head;
private ListNode Tail;
private ListNode Current;
private int ListCountValue;
public void Append(string DataValue ) // 尾部添加数据
{
ListNode NewNode=new ListNode( DataValue);
if (IsNull()) //如果头指针为空
{Head=NewNode;Tail=NewNode;}
else
{Tail.Next =NewNode;NewNode.Previous =Tail; Tail=NewNode;}
Current=NewNode;
ListCountValue+=1;
}
public void Delete() // 删除当前的数据
{
if ( ! IsNull()) //若为空链表
{
//若删除头
if (IsBof())
{
Head=Current.Next ;
Current=Head;
ListCountValue-=1;
return;
}
//若删除尾
if (IsEof())
{
Tail=Current.Previous ;
Current=Tail;
ListCountValue-=1;
return;
}
//若删除中间数据
Current.Previous.Next =Current.Next ;
Current=Current.Previous ;
ListCountValue-=1;
return;
}
}
public void MoveNext() // 向后移动一个数据
{
if (! IsEof()) Current=Current.Next ;
}
public void MovePrevious() /// 向前移动一个数据
{if (!IsBof()) Current=Current.Previous;}
public void MoveFrist() /// 移动到第一个数据
{Current=Head;}
public void MoveLast()/// 移动到最后一个数据
{Current=Tail;}
public bool IsNull()/// 判断是否为空链表
{
if (ListCountValue==0)
return true;
return false;
}
public bool IsEof()/// 判断是否为到达尾部
{
if( Current ==Tail )
return true;
return false;
}
public bool IsBof()/// 判断是否为到达头部
{
if( Current ==Head)
return true;
return false;
}
public string GetCurrentValue()
{return Current.Value ;}
public int ListCount/// 取得链表的数据个数
{get{return ListCountValue;}}
public void Clear() /// 清空链表
{
MoveFrist();
while (!IsNull())
{
//若不为空链表,从尾部删除
Delete();
}
}
public void Insert(string DataValue)/// 在当前位置前插入数据
{
ListNode NewNode=new ListNode (DataValue);
if(IsNull())
{
//为空表,则添加
Append(DataValue);
return;
}
if (IsBof())
{
//为头部插入
NewNode.Next =Head;
Head.Previous =NewNode;
Head=NewNode;
Current=Head;
ListCountValue+=1;
return;
}
//中间插入
NewNode.Next =Current;
NewNode.Previous =Current.Previous ;
Current.Previous.Next =NewNode;
Current.Previous =NewNode;
Current=NewNode;
ListCountValue+=1;
}
public void InsertNode(int position,string nodevalue)
{
if(position >= this.ListCount){this.Append(nodevalue);return;}
this.MoveFrist();
for(int i=0;ithis.ListCount) return null;
this.MoveFrist();
for(int i=0;ithis.ListCount)return;
this.MoveFrist();
for(int i=0;i this.GetCurEntropy()))
{
//满足条件,则插入,退出
Insert(name,entropy);
return;
}
while(true)
{
if (entropy > this.GetCurEntropy())
{
//满族条件,则插入,退出
Insert(name,entropy);
break;
}
if (IsEof())
{
//尾部添加
Append(name,entropy);
break;
}
//移动到下一个指针
MoveNext();
}
}
public string GetHeadAttr(){return this.Head.Attribute_Name;}
public double GetHeadEntropy(){return this.Head.Entropy;}
public void DelHead() //删除头节点
{
if(!this.IsNull())
{
this.MoveFrist();
Head=Current.Next ;
Current=Head;
ListCountValue-=1;
return;
}
}
public string PrintNodeFull()
{
string result=null;
this.MoveFrist();
while (!IsEof())
{result=result+this.GetCurrentAttribute()+this.GetCurEntropy()+"-----"; this.MoveNext();}
return result+this.GetCurrentAttribute()+this.GetCurEntropy();
}
}
}