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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 16:18:09
字體:
供稿:網(wǎng)友
這篇文章主要介紹了JavaScript實現(xiàn)的鏈表數(shù)據(jù)結(jié)構(gòu)實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
 

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

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

 

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

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

 

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 卢氏县| 奇台县| 琼中| 临澧县| 通渭县| 特克斯县| 南昌市| 九江市| 搜索| 平定县| 长白| 巴彦县| 潮州市| 鲁山县| 革吉县| 屯昌县| 元谋县| 衡山县| 淄博市| 泽州县| 尼玛县| 辽源市| 临邑县| 鄯善县| 中阳县| 五常市| 磴口县| 鄂托克前旗| 怀来县| 翁牛特旗| 柏乡县| 若羌县| 镇巴县| 德兴市| 乌拉特后旗| 武安市| 嘉善县| 开封县| 南乐县| 马关县| 年辖:市辖区|