C#數據結構篇(一鏈表類) killertang(原作)
2024-07-21 02:22:15
供稿:網友
c#數據結構篇(一)線性表
作者: 寒羽狼 (dark_slaer_tang)
最近,馬子跑了,你說女人老是容易翻臉。。。,看來做程序員必定要 “煢煢孑立,行影相吊”悲慘命運了。還是老老實實編程吧,我發現用c# 編一些數據接結構的類也瞞不錯的,于是想把數據結構的算法,用c#重寫一遍,打發無聊的時光,下面是數據結構中的鏈表的實現。
首先定義結點類型,定義了,前一個指針域,后一個指針域,如下:
using system;
namespace list
{
/// <summary>
/// summary description for listnode.
/// </summary>
// 結點類
public class listnode
{
public listnode(int newvalue)
{
value=newvalue;
}
/// <summary>
/// 前一個
/// </summary>
public listnode previous;
/// <summary>
/// 后一個
/// </summary>
public listnode next;
/// <summary>
/// 值
/// </summary>
public int value;
}
}
using system;
namespace list
{
/// <summary>
/// 鏈表類
/// </summary>
定義結點之后,開始類線性表的操作編程了.在list 類中,采用了,head ,tail, current,三個指針,使用append ,movefrist,moveprevious,movenext,movelast ,delete,insertascending,insertunascending ,clear 實現移動,添加,刪除,升序插入,降序插入,清空鏈表操作,getcurrentvalue() 方法取得當前的值。
public class clist
{
public clist()
{
//構造函數
//初始化
listcountvalue=0;
head=null;
tail=null;
}
/// <summary>
/// 頭指針
/// </summary>
private listnode head;
/// <summary>
/// 尾指針
/// </summary>
private listnode tail;
/// <summary>
/// 當前指針
/// </summary>
private listnode current;
/// <summary>
/// 鏈表數據的個數
/// </summary>
private int listcountvalue;
/// <summary>
/// 尾部添加數據
/// </summary>
public void append(int 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;
}
/// <summary>
/// 刪除當前的數據
/// </summary>
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;
}
}
/// <summary>
/// 向后移動一個數據
/// </summary>
public void movenext()
{
if (! iseof()) current=current.next ;
}
/// <summary>
/// 向前移動一個數據
/// </summary>
public void moveprevious()
{
if (!isbof()) current=current.previous ;
}
/// <summary>
/// 移動到第一個數據
/// </summary>
public void movefrist()
{
current=head;
}
/// <summary>
/// 移動到最后一個數據
/// </summary>
public void movelast()
{
current=tail;
}
/// <summary>
/// 判斷是否為空鏈表
/// </summary>
public bool isnull()
{
if (listcountvalue==0)
return true;
return false;
}
/// <summary>
/// 判斷是否為到達尾部
/// </summary>
public bool iseof()
{
if( current ==tail )
return true;
return false;
}
/// <summary>
/// 判斷是否為到達頭部
/// </summary>
public bool isbof()
{
if( current ==head)
return true;
return false;
}
public int getcurrentvalue()
{
return current.value ;
}
/// <summary>
/// 取得鏈表的數據個數
/// </summary>
public int listcount
{
get
{
return listcountvalue;
}
}
/// <summary>
/// 清空鏈表
/// </summary>
public void clear()
{
movefrist();
while (!isnull())
{
//若不為空鏈表,從尾部刪除
delete();
}
}
/// <summary>
/// 在當前位置前插入數據
/// </summary>
public void insert(int 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;
}
/// <summary>
/// 進行升序插入
/// </summary>
public void insertascending(int insertvalue)
{
//參數:insertvalue 插入的數據
//為空鏈表
if (isnull())
{
//添加
append(insertvalue);
return;
}
//移動到頭
movefrist();
if ((insertvalue<getcurrentvalue()))
{
//滿足條件,則插入,退出
insert(insertvalue);
return;
}
while(true)
{
if (insertvalue<getcurrentvalue())
{
//滿族條件,則插入,退出
insert(insertvalue);
break;
}
if (iseof())
{
//尾部添加
append(insertvalue);
break;
}
//移動到下一個指針
movenext();
}
}
/// <summary>
/// 進行降序插入
/// </summary>
public void insertunascending(int insertvalue)
{
//參數:insertvalue 插入的數據
//為空鏈表
if (isnull())
{
//添加
append(insertvalue);
return;
}
//移動到頭
movefrist();
if (insertvalue>getcurrentvalue())
{
//滿足條件,則插入,退出
insert(insertvalue);
return;
}
while(true)
{
if (insertvalue>getcurrentvalue())
{
//滿族條件,則插入,退出
insert(insertvalue);
break;
}
if (iseof())
{
//尾部添加
append(insertvalue);
break;
}
//移動到下一個指針
movenext();
}
}
}
}
好了,一個簡單的鏈表類實現了,當然還有許多的功能,可以根據自己的需要添加就好了。to be continue 。