当前位置: 首页 > news >正文

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

文章目录

  • 数据结构和算法,单链表的实现(kotlin版)
    • b站视频链接
    • 1.定义接口,我们需要实现的方法
    • 2.定义节点,表示每个链表节点。
    • 3.push(e: E),链表尾部新增一个节点
    • 4.size(): Int,返回链表的长度
    • 5.getValue(index: Int): E?,获取列表的value值
    • 6.insert(index: Int,e: E),从任意位置插入一个节点
    • 7.remove(index: Int),任意位置删除一个节点
    • 8.完整Demo

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

b站视频链接

单链表的实现–koltin版本

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

interface LinkedListAction<E> {fun push(e: E)fun size(): Intfun 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 = newNodelen++}

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 = headhead = newNodenewNode.next = h} else {//考虑最后一个位置val prev = node(index - 1)val next = prev?.nextprev?.next = newNodenewNode.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 = headhead = h?.nexth?.next = null} else {val prev = node(index - 1)val current = prev?.nextprev?.next = current?.nextcurrent?.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 day1class LinkedList<E> : LinkedListAction<E> {//头指针private var head: Node<E>? = null//优化时间复杂度private var last: Node<E>? = null//集合的长度private var len = 0override 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 = newNodelen++}//找到对应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 = headhead = newNodenewNode.next = h} else {//考虑最后一个位置val prev = node(index - 1)val next = prev?.nextprev?.next = newNodenewNode.next = next}len++}override fun remove(index: Int) {if (index < 0 || index >= len) {throw ArrayIndexOutOfBoundsException("数组越界.....")}if (index == 0) {val h = headhead = h?.nexth?.next = null} else {val prev = node(index - 1)val current = prev?.nextprev?.next = current?.nextcurrent?.next = null}len--}}
http://www.lryc.cn/news/389778.html

相关文章:

  • Jdk17是否有可能代替 Jdk8
  • oca和 ocp有什么区别
  • 煤矿安全大模型:微调internlm2模型实现针对煤矿事故和煤矿安全知识的智能问答
  • C++中的C++中的虚析构函数的作用和重要性
  • 机器学习 - 文本特征处理之 TF 和 IDF
  • 因为自己淋过雨所以想给嵌入式撑把伞
  • 《C++20设计模式》中单例模式
  • 前端技术(说明篇)
  • 带电池监控功能的恒流直流负载组
  • 关于Disruptor监听策略
  • 大数据面试题之HBase(3)
  • c#中赋值、浅拷贝和深拷贝
  • 旧版st7789屏幕模块 没有CS引脚的天坑 已解决!!!
  • 激光粒度分析仪校准步骤详解:提升测量精度的秘诀
  • 独一无二的设计模式——单例模式(python实现)
  • 第二证券:可转债基础知识?想玩可转债一定要搞懂的交易规则!
  • 原型模式的实现
  • 【第二套】华为 2024 年校招-硬件电源岗
  • Xilinx FPGA:vivado利用单端RAM/串口传输数据实现自定义私有协议
  • Spark on k8s 源码解析执行流程
  • 粤港联动,北斗高质量国际化发展的重要机遇
  • Chrome导出cookie的实战教程
  • 视频文字转语音经验笔记
  • 视频融合共享平台LntonCVS统一视频接入平台智慧安防应用方案
  • 使用Python绘制动态螺旋线:旋转动画效果
  • Symfony实战手册:PHP框架的高级应用技巧
  • TOGAF培训什么内容?参加TOGAF培训有什么好处?考试通过率多少?
  • keepalived HA nginx方案
  • 报错:pathspec ‘xxx‘ did not match any file(s) known to git
  • sed 保持空间命令之 x 的执行逻辑