An example of a list of data structures implemented by JavaScript

Source: Internet
Author: User
Tags data structures sort

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.