<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function ListNode(value){
this.val = value
this.pre = null
this.next = null
}
class List {
constructor(){
this.HeadList = new ListNode('head')
}
find(item){
let cur = this.HeadList
while(cur.val != item){
cur = cur.next
}
return cur
}
insert(node,item){
let newnode = new ListNode(node)
let cur = this.find(item)
newnode.next = cur.next
newnode.pre = cur
cur.next = newnode
if(newnode.next){
newnode.next.pre = newnode
}
}
display(){
let cur = this.HeadList
while(cur.next){
console.log(cur.next.val)
cur = cur.next
}
}
remove(item){
let cur = this.find(item)
if(cur.next){
cur.pre.next = cur.next
cur.next.pre = cur.pre
cur.next = null
cur.pre = null
}else{
cur.pre.next = null
cur.next = null
}
}
findLast(){
let cur = this.HeadList
while(cur.next){
cur = cur.next
}
return cur
}
rev(){
let cur = this.findLast()
while(cur.pre){
console.log(cur.val)
cur = cur.pre
}
}
}
let baic = new List()
baic.insert('1','head')
baic.insert('2','1')
baic.insert('3','2')
baic.insert('4','1')
baic.display()
console.log(baic,'^^__^^')
baic.remove('4')
baic.display()
console.log('&&&&&')
baic.rev()
</script>
</body>
</html>