JavaScript链表结构——双向链表
function DoublyLinkList(){
function Node(data
){
this.data
= data
this.pre
= null
this.next
= null
}
this.head
= null
this.tail
= null
this.length
= 0
DoublyLinkList
.prototype
.append = function(data
){
let node
= new Node(data
)
if (this.length
== 0){
this.head
= node
this.tail
= node
}
else{
node
.prev
= this.tail
this.tail
.next
= node
this.tail
= node
}
this.length
+= 1
}
DoublyLinkList
.prototype
.toString = function(){
return this.backwardString()
}
DoublyLinkList
.prototype
.forwardString = function(){
let listString
= ''
let current
= this.tail
while(current
!= null){
listString
+= current
.data
+ ' '
current
= current
.prev
}
return listString
}
DoublyLinkList
.prototype
.backwardString = function(){
let listString
= ''
let current
= this.head
while(current
!= null){
listString
+= current
.data
+ ' '
current
= current
.next
}
return listString
}
DoublyLinkList
.prototype
.insert = function(data
, position
){
if (position
< 0 || position
> this.length
){
return false
}
let node
= new Node(data
)
if (position
== 0){
if (this.length
== 0){
this.tail
= node
}
else{
this.head
.prev
= node
node
.next
= this.head
}
this.head
= node
}
else if (position
== this.length
){
this.append(data
)
}
else{
let current
= null
if (position
<= this.length
/2 - 1){
current
= this.head
let index
= 0
while (index
< position
){
current
= current
.next
index
++
}
}
else {
current
= this.tail
let index
= this.length
- 1
while (index
> position
){
current
= current
.prev
index
--
}
}
node
.next
= current
node
.prev
= current
.prev
current
.prev
.next
= node
current
.prev
= node
}
this.length
+= 1
}
DoublyLinkList
.prototype
.get = function(position
){
if (position
< 0 || position
>= this.length
){
return null
}
let current
= null
if (position
<= this.length
/2 -1){
current
= this.head
let index
= 0
while (index
< position
){
current
= current
.next
index
++
}
}
else {
current
= this.tail
let index
= this.length
- 1
while (index
> position
){
current
= current
.prev
index
--
}
}
return current
.data
}
DoublyLinkList
.prototype
.indexOf = function(data
){
let current
= this.head
let index
= 0
while (current
!= null){
if (current
.data
== data
){
return index
}
current
= current
.next
index
++
}
return -1
}
DoublyLinkList
.prototype
.update = function(newData
, position
){
if (position
< 0 || position
>= this.length
){
return false
}
let current
= null
if (position
<= this.length
/2 -1){
current
= this.head
let index
= 0
while (index
< position
){
current
= current
.next
index
++
}
}
else {
current
= this.tail
let index
= this.length
- 1
while (index
> position
){
current
= current
.prev
index
--
}
}
console
.log(current
.data
+ '=>' + newData
)
current
.data
= newData
}
DoublyLinkList
.prototype
.removeAt = function(position
){
if (position
< 0 || position
>= this.length
){
return null
}
let current
= this.head
if (position
== 0){
if (this.length
== 1){
this.head
= null
this.tail
= null
}
else {
this.head
.next
.prev
= null
this.head
= this.head
.next
}
}
else if (position
== this.length
-1){
current
= this.tail
this.tail
.prev
.next
= null
this.tail
= this.tail
.prev
}
else {
if (position
<= this.length
-1){
current
= this.head
let index
= 0
while (index
< position
){
current
= current
.next
index
++
}
}
else {
current
= this.tail
let index
= this.length
-1
while (index
> position
){
current
= current
.prev
index
--
}
}
current
.prev
.next
= current
.next
current
.next
.prev
= current
.prev
}
this.length
-= 1
return current
.data
}
DoublyLinkList
.prototype
.remove = function(data
){
let index
= this.indexOf(data
)
return this.removeAt(index
)
}
}
转载请注明原文地址: https://mac.8miu.com/read-59682.html