数据结构和算法,单链表的实现(kotlin版)

数据结构和算法,单链表的实现(kotlin版)

b站视频链接

单链表的实现–koltin版本

1.定义接口,我们需要实现的方法

interface LinkedListAction<E> {
    fun push(e: E)
    fun size(): Int
    fun getValue(index: Int): E?
    fun insert(index: Int,e: E)
    fun remove(index: Int)
}

2.定义节点,表示每个链表节点。

data class Node<E>(var next: Node<E>? = null, var value: E)

3.push(e: E),链表尾部新增一个节点

override fun push(e: E) {
        val newNode = Node(null, e)
        if (head != null) {
//            val lastNode = node(len - 1)
            //O(1)时间复杂度
            last?.next = newNode
        } else {
            head = newNode
        }
        last = newNode
        len++
    }

4.size(): Int,返回链表的长度

override fun size(): Int {
        return len
    }

5.getValue(index: Int): E?,获取列表的value值

    override fun getValue(index: Int): E? {
        if (index < 0 || index >= len) {
            throw ArrayIndexOutOfBoundsException("数组越界.....")
        }
        return node(index)?.value
    }
	//找到对应index下标的节点。
    private fun node(index: Int): Node<E>? {
        var h = head
        //O(n)时间复杂度
        for (i in 0 until index) {
            h = h?.next
        }
        return h
    }

6.insert(index: Int,e: E),从任意位置插入一个节点

override fun insert(index: Int, e: E) {
        val newNode = Node(null, e)
        //考虑边界
        if (index == 0) {
            val h = head
            head = newNode
            newNode.next = h
        } else {
            //考虑最后一个位置
            val prev = node(index - 1)
            val next = prev?.next
            prev?.next = newNode
            newNode.next = next
        }
        len++
    }
    //找到对应index下标的节点。
    private fun node(index: Int): Node<E>? {
        var h = head
        //O(n)时间复杂度
        for (i in 0 until index) {
            h = h?.next
        }
        return h
    }

7.remove(index: Int),任意位置删除一个节点

override fun remove(index: Int) {
        if (index < 0 || index >= len) {
            throw ArrayIndexOutOfBoundsException("数组越界.....")
        }
        if (index == 0) {
            val h = head
            head = h?.next
            h?.next = null
        } else {
            val prev = node(index - 1)
            val current = prev?.next
            prev?.next = current?.next
            current?.next = null
        }
        len--
    }
    //找到对应index下标的节点。
    private fun node(index: Int): Node<E>? {
        var h = head
        //O(n)时间复杂度
        for (i in 0 until index) {
            h = h?.next
        }
        return h
    }

8.完整Demo

package day1

class LinkedList<E> : LinkedListAction<E> {
    //头指针
    private var head: Node<E>? = null
    //优化时间复杂度
    private var last: Node<E>? = null
    //集合的长度
    private var len = 0
    override fun push(e: E) {
        val newNode = Node(null, e)
        if (head != null) {
//            val lastNode = node(len - 1)
            //O(1)时间复杂度
            last?.next = newNode
        } else {
            head = newNode
        }
        last = newNode
        len++
    }

    //找到对应index下标的节点。
    private fun node(index: Int): Node<E>? {
        var h = head
        //O(n)时间复杂度
        for (i in 0 until index) {
            h = h?.next
        }
        return h
    }

    override fun size(): Int {
        return len
    }

    override fun getValue(index: Int): E? {
        if (index < 0 || index >= len) {
            throw ArrayIndexOutOfBoundsException("数组越界.....")
        }
        return node(index)?.value
    }

    override fun insert(index: Int, e: E) {
        val newNode = Node(null, e)
        //考虑边界
        if (index == 0) {
            val h = head
            head = newNode
            newNode.next = h
        } else {
            //考虑最后一个位置
            val prev = node(index - 1)
            val next = prev?.next
            prev?.next = newNode
            newNode.next = next
        }
        len++
    }

    override fun remove(index: Int) {
        if (index < 0 || index >= len) {
            throw ArrayIndexOutOfBoundsException("数组越界.....")
        }
        if (index == 0) {
            val h = head
            head = h?.next
            h?.next = null
        } else {
            val prev = node(index - 1)
            val current = prev?.next
            prev?.next = current?.next
            current?.next = null
        }
        len--
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值