function Node(element){
this.val=element;
this.next=null;
this.prev=null;
}
function Links(){
this.head=new Node("Head");
this.lastNode=this.head;
}
Links.prototype={
//插入链表 按位置插入 或者 插入到末尾
insert:function(element,location){
var newNode=new Node(element);
this.lastNode.next=newNode;
newNode.prev=this.lastNode;
this.lastNode=newNode;
return newNode;
},
//查找链表
findNode:function(keyword){
var node = this.head;
if(!node.next){
console.log("链表为空")
return false;
}
var tempStr="";
while(node.val!=keyword){
node=node.next;
}
return node;
},
//删除节点
removeNode:function(keyword){
var node = this.findNode(keyword);
if(node.next!=undefined){
node.prev.next=node.next;
node.next.prev=node.prev;
}
//删除最后一个元素更新
if(this.lastNode==node){
node.prev.next=null;
this.lastNode=node.prev;
};
node.next=null;
node.prev=null;
node=null;
},
//打印链表
print:function(){
var node = this.head;
if(!node.next){
console.log("链表为空")
return false;
}
var tempStr="";
while(node!=undefined){
tempStr+=node.val+"-->";
node=node.next;
}
console.log(tempStr+"null")
},
//反向输出
reversePrint:function(){
var node=this.lastNode;
var tempStr="last-->";
while(node!=undefined){
tempStr+=node.val+"-->";
node=node.prev;
}
console.log(tempStr+"null")
},
//查找最后一个元素
lastNode:function(){
return this.lastNode;
}
}
var list=new Links();
list.insert("看下")
list.insert("是不")
list.insert("1")
list.insert("2")
list.removeNode("2");
list.print();
list.reversePrint();