手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JavaScript实现的链表数据结构实例
JavaScript实现的链表数据结构实例
摘要:此例是javascript来建立链表。。并对此进行了排序。。还可以在GenericList一般链表上进行扩展。实现各种排序及增,删,改结点。...

此例是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")

【JavaScript实现的链表数据结构实例】相关文章:

使用JavaScript刷新网页的方法

JavaScript的9种继承实现方式归纳

在HTML中插入JavaScript代码的示例

JavaScript中fixed()方法的使用简介

JavaScript中Number.MIN_VALUE属性的使用示例

Javascript实现div层渐隐效果的方法

js实现简单锁屏功能实例

javascript实现链接单选效果

JavaScript中的私有成员

javascript实现淡蓝色的鼠标拖动选择框实例

精品推荐
分类导航