let p=this.head;
while( i++ < Pos && p){
if(i===Pos){
return p.data;
}
p=p.next;
}
console.log(new Error(‘未找到该位置’));
}
七、删除结点
//按指定位置删除结点
LinkList.prototype.deleteData=function(Pos){
if(!this.head){
console.log(new Error(‘链表为空’));
return;
}
let p=this.head;
let i=1;
//如果删除的是头结点
if(Pos===1){
p=p.next;
this.head=p;
return;
}
while(p.next){
if(i===Pos-1){
let r=p.next;
p.next=r.next;
return;
}
i++;
p=p.next;
}
console.log(new Error(‘未找到该位置’));
}
八、清空链表
//清空链表
LinkList.prototype.clear=function(){
let q=null;
while(this.head){
q=this.head.next;
this.head=null;
this.head=q;
}
}
附:完整源码:
‘use strict’
function LinkList(){
//初始化头结点为null
this.head=null;
//内部类,储存结点数据和next
function Node(data){