|
Posted on
2006-08-14 15:29
Leo.Zhu
阅读( 8519)
评论()
编辑
收藏
举报
using System;
namespace List
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// Summary description for ListNode.
/// </summary>
// 结点类
public class ListNode
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
public ListNode(int NewValue)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
Value=NewValue;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 前一个
/// </summary>
public ListNode Previous;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 后一个
/// </summary>
public ListNode Next;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 值
/// </summary>
public int Value;
}
}
using System;
namespace List
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 链表类
/// </summary>
定义结点之后,开始类线性表的操作编程了.在LIST 类中,采用了,Head ,Tail, Current,三个指针,使用Append ,MoveFrist,MovePrevious,MoveNext,MoveLast ,Delete,InsertAscending,InsertUnAscending ,Clear 实现移动,添加,删除,升序插入,降序插入,清空链表操作,GetCurrentValue() 方法取得当前的值。
public class Clist
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
public Clist()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//构造函数
//初始化
ListCountValue=0;
Head=null;
Tail=null;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 头指针
/// </summary>
private ListNode Head;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 尾指针
/// </summary>
private ListNode Tail;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 当前指针
/// </summary>
private ListNode Current;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 链表数据的个数
/// </summary>
private int ListCountValue;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 尾部添加数据
/// </summary>
public void Append(int DataValue )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
ListNode NewNode=new ListNode( DataValue);
if (IsNull())
//如果头指针为空
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
Head=NewNode;
Tail=NewNode;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
Tail.Next =NewNode;
NewNode.Previous =Tail;
Tail=NewNode;
}
Current=NewNode;
//链表数据个数加一
ListCountValue+=1;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 删除当前的数据
/// </summary>
public void Delete()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//若为空链表
if ( ! IsNull())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//若删除头
if (IsBof())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
Head=Current.Next ;
Current=Head;
ListCountValue-=1;
return;
}
//若删除尾
if (IsEof())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
Tail=Current.Previous ;
Current=Tail;
ListCountValue-=1;
return;
}
//若删除中间数据
Current.Previous.Next =Current.Next ;
Current=Current.Previous ;
ListCountValue-=1;
return;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 向后移动一个数据
/// </summary>
public void MoveNext()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
if (! IsEof()) Current=Current.Next ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 向前移动一个数据
/// </summary>
public void MovePrevious()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
if (!IsBof()) Current=Current.Previous ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 移动到第一个数据
/// </summary>
public void MoveFrist()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
Current=Head;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 移动到最后一个数据
/// </summary>
public void MoveLast()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
Current=Tail;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 判断是否为空链表
/// </summary>
public bool IsNull()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
if (ListCountValue==0)
return true;
return false;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 判断是否为到达尾部
/// </summary>
public bool IsEof()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
if( Current ==Tail )
return true;
return false;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 判断是否为到达头部
/// </summary>
public bool IsBof()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
if( Current ==Head)
return true;
return false;
}
public int GetCurrentValue()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
return Current.Value ;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 取得链表的数据个数
/// </summary>
public int ListCount
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
return ListCountValue;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 清空链表
/// </summary>
public void Clear()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
MoveFrist();
while (!IsNull())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//若不为空链表,从尾部删除
Delete();
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 在当前位置前插入数据
/// </summary>
public void Insert(int DataValue)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
ListNode NewNode=new ListNode (DataValue);
if(IsNull())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//为空表,则添加
Append(DataValue);
return;
}
if (IsBof())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//为头部插入
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;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 进行升序插入
/// </summary>
public void InsertAscending(int InsertValue)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//参数:InsertValue 插入的数据
//为空链表
if (IsNull())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//添加
Append(InsertValue);
return;
}
//移动到头
MoveFrist();
if ((InsertValue<GetCurrentValue()))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//满足条件,则插入,退出
Insert(InsertValue);
return;
}
while(true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
if (InsertValue<GetCurrentValue())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//满族条件,则插入,退出
Insert(InsertValue);
break;
}
if (IsEof())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//尾部添加
Append(InsertValue);
break;
}
//移动到下一个指针
MoveNext();
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 进行降序插入
/// </summary>
public void InsertUnAscending(int InsertValue)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//参数:InsertValue 插入的数据
//为空链表
if (IsNull())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//添加
Append(InsertValue);
return;
}
//移动到头
MoveFrist();
if (InsertValue>GetCurrentValue())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//满足条件,则插入,退出
Insert(InsertValue);
return;
}
while(true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
if (InsertValue>GetCurrentValue())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//满族条件,则插入,退出
Insert(InsertValue);
break;
}
if (IsEof())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](/Images/OutliningIndicators/ContractedSubBlock.gif) {
//尾部添加
Append(InsertValue);
break;
}
//移动到下一个指针
MoveNext();
}
}
}
}
![](/Images/OutliningIndicators/None.gif)
|