链表:依靠一个节点的来寻找下一个节点;
故在设计的时候,首先应该定义一个头节点;
function Node(ele){
this.ele=ele;
this.next=null;
}
在上面代码中,ele表示该节点的数据,next是指向下一个节点的指针,这里之所以为null,是因为这是一个头节点,后面的节点还并未设计。
function list(){
this.head=new Node("head")//将头节点设置为head;
this.insert=insert;
this.find=find;
this.remove=remove;
}
function find(ele){
var res=this.head;
while(res.ele!=ele){
res=res.next;
}
return res;
}
function insert(newele,old){
var newnode=new Node(newele);
var res=this.find(old);
newnode.next=res.next;
res.next=newnode;
}