JavaScript实现的链表数据结构实例

此例是javascript来建立链表。。 并对此进行了排序。。

还可以在GenericList一般链表上进行扩展。 实现各种排序及增,删,改结点。。

代码如下:

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

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

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...