使用 scala 实现双向链表

双向链表也叫双链表。双向链表中不仅有指向后一个节点的指针,还有指向前一个节点的指针。这样可以从任何一个节点访问前一个节点,当然也可以访问后一个节点,以至整个链表。
一般是在需要大批量的另外储存数据在链表中的位置的时候用。双向链表也可以
在这里插入图片描述

package com.atguigu.datastruc.linked_list

/**
  * Author lzc
  * Date 2019-11-27 11:11
  */
object DoublyLinkedListDemo {
    def main(args: Array[String]): Unit = {
        val list = new DoublyLinkedList[Int]()
        list.add(10)
        list.add(20)
        list.add(30)
        
        list.printInfo()
        list.delete(30)
        list.delete(20)
        list.delete(10)
        list.printInfo()
    }
}

class DoublyLinkedList[T] {
    
    var head: Node = _
    var tail: Node = _
    
    /**
      * 删除节点
      * 双向节点删除比较方便: 支持自删除
      *
      * @param ele
      */
    def delete(ele: T): Boolean = {
        // 找到要删除的节点
        val targetNode: Node = find(ele)
        if (targetNode == null) { // 如果要删除的节点不存在
            false
        }
        else { // 删除的节点存在
            // 上一个节点
            val preNode: Node = targetNode.pre
            val nextNode: Node = targetNode.next // 下一个节点
            
            if (targetNode == head) { // 如果是头节点
                if (nextNode != null) nextNode.pre = null // 下一个节点的 pre 指向 null
                head = nextNode // 更新头节点
            } else if (targetNode == tail) { // 如果是尾节点
                preNode.next = null // 上一个节点的 next 指向 null
                tail = preNode // 更新尾节点
            } else {
                preNode.next = nextNode
                nextNode.pre = preNode
            }
            true
        }
    }
    
    /**
      * 找到要删除的元素所在的节点
      *
      * @param ele
      */
    protected def find(ele: T): Node = {
        var tmp: Node = head // 从头节点开始查找
        while (tmp != null) {
            if (tmp.value == ele) return tmp
            tmp = tmp.next
        }
        null
    }
    
    /**
      * 新增节点
      *
      * @param ele
      */
    def add(ele: T): Boolean = {
        val newNode: Node = Node(ele, null, null)
        if (head == null) { // 第一次添加
            head = newNode // 因为第一个元素, 上一个和下一个节点都应该是 null
            
        } else { // 不是第一次添加, tail 的next节点指向新节点, 新节点的pre节点指向 tail
            tail.next = newNode
            newNode.pre = tail
        }
        // 更新 tail 的指向
        tail = newNode
        true
    }
    
    
    /**
      * 打印链表的元素
      */
    def printInfo(): Unit = {
        if (head == null) return
        
        var tmp: Node = head
        do {
            print(tmp.value + "->")
            tmp = tmp.next
        } while (tmp != null)
        
        println()
    }
    
    case class Node(value: T, var pre: Node, var next: Node)
    
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Scala 实现目录及文件 copy 操作的示例代码: ```scala import java.io._ object FileCopy { def main(args: Array[String]): Unit = { val source = new File("path/to/source/directory") val destination = new File("path/to/destination/directory") copyDirectory(source, destination) } def copyDirectory(source: File, destination: File): Unit = { if (source.isDirectory) { if (!destination.exists()) { destination.mkdir() } val files = source.listFiles() for (f <- files) { val newFile = new File(destination.getAbsolutePath + File.separator + f.getName) copyDirectory(f, newFile) } } else { val in = new FileInputStream(source) val out = new FileOutputStream(destination) val buffer = new Array[Byte](1024) var length = in.read(buffer) while (length > 0) { out.write(buffer, 0, length) length = in.read(buffer) } in.close() out.close() } } } ``` 在上述代码中,我们首先定义了源目录和目标目录,然后调用 `copyDirectory` 方法来执行目录和文件的复制操作。如果源目录是一个目录,我们会检查目标目录是否存在,如果不存在则创建它,并且遍历源目录中的所有文件和子目录。对于每个文件,我们都会创建一个新的文件对象,并递归调用 `copyDirectory` 方法来复制文件到目标目录中。如果源目录是一个文件,我们会使用 `FileInputStream` 和 `FileOutputStream` 来读取和写入文件内容,并使用一个缓冲区来提高性能。最后,我们关闭输入和输出流并退出方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值