<!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>双向链表</title>
</head>
<body>
<script>
function DoubleList(){
this.head=null
this.tail=null
this.length=0
function Node(data){
this.data=data
this.prev=null
this.next=null
}
DoubleList.prototype.append=function(data){
var newNode=new Node(data)
if(this.length==0){
this.head=newNode
}else{
var current=this.head
while(current.next){
current=current.next
}
current.next=newNode
}
this.length+=1
}
DoubleList.prototype.backwardString=function(data){
//定义变量
var current=this.head
var resultString=""
while(current){
resultString+=current.data+" "
current=current.next
}
return resultString
}
DoubleList.prototype.forwardString=function(data){
//定义变量
var current=this.tail
var resultString=""
while(current){
resultString+=current.data+" "
current=current.prev
}
return resultString
}
DoubleList.prototype.toString=function(data){
//定义变量
return this.backwardString()
}
}
</script>
</body>
</html>
数据结构57-双向链表转成字符串代码
最新推荐文章于 2024-09-09 23:14:05 发布