使用js实现链表数据结构(增 删 该 查 打印)

//使用js实现链表:

<script>
function Node(element){
this.element = element;//当前节点数据
this.next = null;//下一个节点
}

function LinkedList(){
this.head = new Node("head");
}
LinkedList.prototype = {
//查找某一节点
find:function(item){
var currNode = this.head;
while(currNode.next != null && currNode.element != item){
currNode = currNode.next;
}
return currNode;
},
//向某一元素后面插入新节点
insert:function(newElement,item){
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
current.next = newNode;
},
//查找某一节点的前一个节点
findPrevious:function(item){
var currNode = this.head;
while(currNode.next != null&&currNode.next.element != item){
currNode = currNode.next;
}
return currNode;
},
//删除某一节点
remove:function(item){
var prevNode = this.findPrevious(item);
if(!(prevNode.next == null)){
prevNode.next = prevNode.next.next;
}
},
//修改某一节点的数据
edit:function(item,newItem){
var node = this.find(item);
node.element = newItem;
},
//在控制台打印出所有节点
display:function(){
var currNode = this.head;
console.log(currNode.element)
while(currNode.next != null){
console.log(currNode.next.element);
currNode = currNode.next;
}
},
//判断单项链表是否有循环
//设置两个指针互相追逐。一个指针每次前进一步,第二个指针每次前进两步,如果有相遇,则说明有环。
hasCircle() {
var slowPointer = this.head;
var fastPointer = this.head;
while(fastPointer != null){
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
if(slowPointer == fastPointer){
return true;