国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

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ā)資料庫,
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 丰都县| 宜都市| 泰来县| 柳林县| 建宁县| 肃北| 睢宁县| 新竹县| 北川| 桃江县| 高雄市| 呼和浩特市| 应城市| 那坡县| 双流县| 武穴市| 西乌珠穆沁旗| 新兴县| 南阳市| 永清县| 东源县| 兖州市| 明光市| 和田县| 永城市| 罗山县| 锡林浩特市| 鄯善县| 苏州市| 神池县| 东光县| 井陉县| 迁安市| 仁怀市| 贵州省| 淳化县| 沙河市| 宜黄县| 临沂市| 黄梅县| 百色市|