微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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")

原文地址:https://www.jb51.cc/js/54994.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐