JavaScript落到实处的链表数据结构实例
此例是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
{sl.addHead(arguments[i]);
}
alert(“未排序的链表”);
sl.print();
sl.BubbleSort();
alert(“已排序的链表 从小到大”);
sl.print();
})(“1″,”2″,”3″,”4”)
http://www.bkjia.com/Javascript/977613.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javascript/977613.htmlTechArticleJavaScript实现的链表数据结构实例
此例是javascript来树立链表。。 并对此开展了排序。。
还是能够在GenericList一般链表上海展览中心开增添。 达成各样…