C#數(shù)據(jù)結(jié)構(gòu)篇(二 堆棧) killertang(原作)
2024-07-21 02:28:06
供稿:網(wǎng)友
c#數(shù)據(jù)結(jié)構(gòu)篇(二)堆棧
作者: 寒羽狼 (dark_slaer_tang)
煙快沒有了,別人都說煙有害,但對(duì)與我可是必不可少的,呵呵。。,抽一根煙程序就寫出來(lái)了,好了,再加把勁,我們?cè)偻瓿啥褩#鋵?shí)堆棧和線性表沒什么大的區(qū)別,堆棧其實(shí)是一種特殊的線性表,使用push 壓輸入數(shù)據(jù),pop 彈出數(shù)據(jù),才用,先進(jìn)后出的原則,就這么簡(jiǎn)單。前面已經(jīng)寫好了list 類,在這里我們可以調(diào)用它。代碼如下:
using system;
namespace list
{
/// <summary>
/// 堆棧類
/// </summary>
public class cstack
{
//調(diào)用鏈表類
private clist m_list;
public cstack()
{
//構(gòu)造函數(shù)
m_list=new clist();
}
/// <summary>
/// 壓入堆棧
/// </summary>
public void push(int pushvalue)
{
//參數(shù): int pushvalue 壓入堆棧的數(shù)據(jù)
m_list.append (pushvalue);
}
/// <summary>
/// 彈出堆棧數(shù)據(jù),如果為空,則取得 2147483647 為 int 的最大值;
/// </summary>
public int pop()
{
//功能:彈出堆棧數(shù)據(jù)
int popvalue;
if (!isnullstack())
{
//不為空堆棧
//移動(dòng)到頂
movetop();
//取得彈出的數(shù)據(jù)
popvalue=getcurrentvalue();
//刪除
delete();
return popvalue;
}
// 空的時(shí)候?yàn)?int 類型的最大值
return 2147483647;
}
/// <summary>
/// 判斷是否為空的堆棧
/// </summary>
public bool isnullstack()
{
if ( m_list.isnull() )
return true ;
return false ;
}
/// <summary>
/// 堆棧的個(gè)數(shù)
/// </summary>
public int stacklistcount
{
get
{
return m_list.listcount ;
}
}
/// <summary>
/// 移動(dòng)到堆棧的底部
/// </summary>
public void movebottom()
{
m_list.movefrist ();
}
/// <summary>
/// 移動(dòng)到堆棧的top
/// </summary>
public void movetop()
{
m_list.movelast ();
}
/// <summary>
/// 向上移動(dòng)
/// </summary>
public void moveup()
{
m_list.movenext();
}
/// <summary>
/// 向上移動(dòng)
/// </summary>
public void movedown()
{
m_list.moveprevious() ;
}
/// <summary>
/// 取得當(dāng)前的值
/// </summary>
public int getcurrentvalue()
{
return m_list.getcurrentvalue ();
}
/// <summary>
/// 刪除取得當(dāng)前的結(jié)點(diǎn)
/// </summary>
public void delete()
{
m_list.delete ();
}
/// <summary>
/// 清空堆棧
/// </summary>
public void clear()
{
m_list.clear();
}
}
}
使用先前的鏈表類,就可以輕松的完成堆棧類,除了基本的,pop,push 方法外,還提供,movebottom,movetop,moveup,movedown ,來(lái)訪問堆棧中的數(shù)據(jù),使用getcurrentvalue方法,來(lái)取得數(shù)據(jù)的值,可以執(zhí)行更多的操作,呵呵,就這么簡(jiǎn)單。to be continue.