亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript實(shí)現(xiàn)的鏈表數(shù)據(jù)結(jié)構(gòu)實(shí)例

 更新時(shí)間:2015年04月02日 08:29:35   投稿:junjie  
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的鏈表數(shù)據(jù)結(jié)構(gòu)實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下

此例是javascript來(lái)建立鏈表。。
并對(duì)此進(jìn)行了排序。。

還可以在GenericList一般鏈表上進(jìn)行擴(kuò)展。
實(shí)現(xiàn)各種排序及增,刪,改結(jié)點(diǎn)。。

復(fù)制代碼 代碼如下:

function Node(){
  this.data=null;
  this.next=null;
}

function GenericList(){
  this.head=null;
  this.current=null;
  //打出所有的鏈表結(jié)點(diǎn)
  this.print= function(){
  this.current=this.head;
   while(this.current!=null){
      alert(this.current.data);
      this.current=this.current.next;
    }
 
  },
  //建立鏈表
  this.addHead =function(t){
     
 
      var node=new Node();
      node.data=t;
      node.next=this.head;
      this.head=node;
 
  }
 
 

}


function SortList(){
//冒泡排序鏈表
this.BubbleSort=function()
   {
     if(this.head==null||this.head.next==null)
     {
        return ;
     }
    var swapped;
    do{
   
     this.previous=null;
     this.current=this.head;

     var swapped=false;
     while(this.current.next!=null)
      {
      
       if(this.current.data-this.current.next.data>0)
        {
      
        var tmp=this.current.next;
        this.current.next=this.current.next.next;
        tmp.next=this.current;
        if(this.previous==null)
            {
               this.head=tmp;
            }
         else
           {
               this.previous.next=tmp;
           }
          this.previous=tmp;
          swapped=true;
         
      
       }
       else
        {
       
        this.previous=this.current;
        this.current=this.current.next;
       
        }
    
     }
    
     
   
    }while(swapped);
  
   }

}

SortList.prototype=new GenericList();


(function Main(){
 var sl=new  SortList();
 for(var i=0;i<arguments.length;i++)
 {sl.addHead(arguments[i]);
 }
 alert("未排序的鏈表");
 sl.print();
 sl.BubbleSort();
  alert("已排序的鏈表  從小到大");
 sl.print();

})("1","2","3","4")

相關(guān)文章

最新評(píng)論