Home > Article > Web Front-end > Create a linked list using js objects
//The following is a linked list class
function LinkedList(){
//Node represents the item to be added to the list
var Node=function(element){
<br/>
this.element=element;
this.next= null;
};
var length=0;//Storage the number of list items
var head=null;//head stores a reference to the first node
//Append elements to the end of the linked list
this. append=function(element){
var node=new Node(element),
current;
if(head===null){
head=node;
}else{
current=node;
while( current.next){
current=current.next;
}
current.next=node;
}
length++;
};
//Insert an element at any position in the linked list
this.insert=function (position,element){
if(position>=0&&position643259235a5fb7b06618e6bbbb037e52-1 && position
previous,
index=0;
if(position===0){
head=current.next;
} else{
return this.removeAt(index);
};
//Judge whether the linked list is empty
this .isEmpty=function(){
Return length===0;
};
//Return the length of the linked list
this.size=function(){
return length;
};
//Convert the LinkedList object into a string
var current=head,
string="";
while(current){
string=current.element;
}
return string;};
};
var list=new LinkedList();
list.append(15);
list.append(10);
list.insert(1,11);
console.log(list.size());