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