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 day5
class LinkedList<E>: LinkedListAction<E>{//头节点指针privatevar head: Node<E>?=null//尾部节点指针privatevar last: Node<E>?=null//链表总长度privatevar len =0//尾插法overridefunpush(e: E){linkLast(e)
len++}//尾插法privatefunlinkLast(e: E){//保存last指针val l = last
//新创建的节点val newNode =Node(null, last, e)//last指针指向新节点
last = newNode
//如果head不为空,next赋值为新节点if(head !=null){
l?.next = newNode
}else{//否则头节点为最新节点
head = newNode
}}//返回链表的长度overridefunsize(): Int {return len
}//获取节点的Value值overridefungetValue(index: Int): E?{//判断是否index越界if(index <0|| index >= len){throwArrayIndexOutOfBoundsException("越界异常.............")}//拿到index节点的valuereturnnode(index)?.value
}//获取index对应的节点privatefunnode(index: Int): Node<E>?{//右移一位,相当于 len / 2returnif(index <(len shr1)){//从前面往后便利,临时指针cur指向headvar cur = head
//移动nextfor(i in0 until index){
cur = cur?.next
}//找到对应的index节点
cur
}else{//否则从后往前便利var cur = last
for(i in(len -1) downTo index +1){
cur = cur?.prev
}//找到对应的index节点
cur
}}//指定位置插入节点overridefuninsert(index: Int, e: E){if(index <0|| index >= len){throwArrayIndexOutOfBoundsException("越界异常.............")}//判断插入的index == len就插入在后面if(index == len){linkLast(e)}else{//否则插入对应的节点的位置上linkBefore(node(index), e)}
len++}//链接到对应的节点位置的前面privatefunlinkBefore(cur: Node<E>?, e: E){//保存prev指针val prev = cur?.prev
//创建新节点val newNode =Node(cur, prev, e)//插入到index的节点的前面
cur?.prev = newNode
//prev不为空if(prev !=null){//prev.next接入新节点
prev.next = newNode
}else{//prev为空的话,插入头节点位置
head = newNode
}}//删除元素overridefunremove(index: Int){if(index <0|| index >= len){throwArrayIndexOutOfBoundsException("越界异常.............")}unlike(node(index))
len--}//找到index对应节点删除privatefununlike(cur: Node<E>?): E?{//获取当前节点valuevar value = cur?.value
//获取当前节点的前驱节点val prev = cur?.prev
//获取next指针val next = cur?.next
//判断临界值if(prev !=null){//前驱节点的next指向cur当前节点的next
prev.next = next
}else{//没有前驱节点
head = next
}//判断临界值if(next !=null){//后驱节点的prev指向cur当前节点的prev
next.prev = prev
}else{//没有后驱节点
last = prev
}return value
}}