This example is JavaScript to create a linked list ...
And sort this out ...
It can also be extended on the GenericList general list.
To achieve a variety of sorting and increase, delete, change node.
The code is as follows:
function Node () {
This.data=null;
This.next=null;
}
function GenericList () {
This.head=null;
This.current=null;
All the linked list nodes are played.
this.print= function () {
This.current=this.head;
while (This.current!=null) {
alert (this.current.data);
This.current=this.current.next;
}
},
Set up a linked list
This.addhead =function (t) {
var node=new node ();
node.data=t;
Node.next=this.head;
This.head=node;
}
}
function Sortlist () {
Bubble Sort List
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 ("unordered list");
Sl.print ();
Sl. Bubblesort ();
Alert ("sorted list from small to large");
Sl.print ();
}) ("1", "2", "3", "4")