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

首頁 > 編程 > JavaScript > 正文

JavaScript實現的鏈表數據結構實例

2019-11-20 12:47:44
字體:
來源:轉載
供稿:網友

此例是javascript來建立鏈表。。
并對此進行了排序。。

還可以在GenericList一般鏈表上進行擴展。
實現各種排序及增,刪,改結點。。

復制代碼 代碼如下:

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

function GenericList(){
  this.head=null;
  this.current=null;
  //打出所有的鏈表結點
  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")

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临武县| 遂溪县| 乐清市| 襄汾县| 汕尾市| 奉节县| 高碑店市| 通渭县| 盐津县| 沾益县| 南和县| 依安县| 余姚市| 天津市| 大城县| 延边| 金沙县| 临武县| 金塔县| 旺苍县| 如皋市| 尼玛县| 奉贤区| 湟中县| 武汉市| 汽车| 绩溪县| 贵阳市| 新疆| 大荔县| 宜兴市| 新巴尔虎右旗| 邯郸县| 晋中市| 连江县| 卢龙县| 洪湖市| 西吉县| 陕西省| 阿图什市| 铜梁县|