overridefunpush(e: E){linkLast(e)
len++}privatefunlinkLast(e: E){var l = last
val newNode =Node(null, last, e)
last = newNode
if(head !=null){
l?.next = newNode
}else{
head = newNode
}}
4.size(): Int,返回链表的长度
overridefunsize(): Int {return len
}
5.getValue(index: Int): E?,获取列表的value值
overridefungetValue(index: Int): E?{if(index <0|| index >= len){throwArrayIndexOutOfBoundsException("数组越界.....")}returnnode(index)?.value
}//找到对应index下标的节点。privatefunnode(index: Int): Node<E>?{if(index <(len shr1)){var cur = head
for(i in0 until index){
cur = cur?.next
}return cur
}else{var cur = last
for(i in(len -1) downTo index +1){
cur = cur?.prev
}return cur
}}
6.insert(index: Int,e: E),从任意位置插入一个节点
overridefuninsert(index: Int, e: E){if(index <0|| index > len){throwArrayIndexOutOfBoundsException("数组越界.....")}if(index == len){linkLast(e)}else{linkBefore(node(index), e)}
len++}privatefunlinkLast(e: E){var l = last
val newNode =Node(null, last, e)
last = newNode
if(head !=null){
l?.next = newNode
}else{
head = newNode
}}privatefunlinkBefore(cur: Node<E>?, e: E){val prev = cur?.prev
val newNode =Node(cur, prev, e)
cur?.prev = newNode
if(prev !=null){
prev.next = newNode
}else{
head = newNode
}}
7.remove(index: Int),任意位置删除一个节点
overridefunremove(index: Int){if(index <0|| index >= len){throwArrayIndexOutOfBoundsException("数组越界.....")}unlike(node(index))
len--}privatefununlike(cur: Node<E>?): E?{val value = cur?.value
val prev = cur?.prev
val next = cur?.next
if(prev !=null){
prev.next = next
}else{
head = next
}if(next !=null){
next.prev = prev
}else{
last = prev
}return value
}
8.完整Demo
package day1
class LinkedList<E>: LinkedListAction<E>{//头指针privatevar head: Node<E>?=null//优化时间复杂度privatevar last: Node<E>?=null//集合的长度privatevar len =0overridefunpush(e: E){linkLast(e)
len++}privatefunlinkLast(e: E){var l = last
val newNode =Node(null, last, e)
last = newNode
if(head !=null){
l?.next = newNode
}else{
head = newNode
}}//找到对应index下标的节点。privatefunnode(index: Int): Node<E>?{if(index <(len shr1)){var cur = head
for(i in0 until index){
cur = cur?.next
}return cur
}else{var cur = last
for(i in(len -1) downTo index +1){
cur = cur?.prev
}return cur
}}overridefunsize(): Int {return len
}overridefungetValue(index: Int): E?{if(index <0|| index >= len){throwArrayIndexOutOfBoundsException("数组越界.....")}returnnode(index)?.value
}overridefuninsert(index: Int, e: E){if(index <0|| index > len){throwArrayIndexOutOfBoundsException("数组越界.....")}if(index == len){linkLast(e)}else{linkBefore(node(index), e)}
len++}privatefunlinkBefore(cur: Node<E>?, e: E){val prev = cur?.prev
val newNode =Node(cur, prev, e)
cur?.prev = newNode
if(prev !=null){
prev.next = newNode
}else{
head = newNode
}}overridefunremove(index: Int){if(index <0|| index >= len){throwArrayIndexOutOfBoundsException("数组越界.....")}unlike(node(index))
len--}privatefununlike(cur: Node<E>?): E?{val value = cur?.value
val prev = cur?.prev
val next = cur?.next
if(prev !=null){
prev.next = next
}else{
head = next
}if(next !=null){
next.prev = prev
}else{
last = prev
}return value
}}