Data structure implemented by JavaScript
In this example, JavaScript is used to create a linked list ..
And sorted ..
It can also be expanded on the genericlist General linked list.
Sort, add, delete, and modify nodes ..
<SCRIPT>
Function node (){
This. Data = NULL;
This. Next = NULL;
}
Function genericlist (){
This. Head = NULL;
This. Current = NULL;
// Print all linked list nodes
This. Print = function (){
This. Current = This. head;
While (this. Current! = NULL ){
Alert (this. Current. data );
This. Current = This. Current. Next;
}
},
// Create 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 linked 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. Present. 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 ("unordered Linked List ");
SL. Print ();
SL. bubblesort ();
Alert ("sorted linked lists from small to large ");
SL. Print ();
}) ("1", "2", "3", "4 ")
</SCRIPT>