commit 65431b082211be232da26901e8f620fc016c1654 Author: XiaoLFeng Date: Tue Nov 21 16:04:32 2023 +0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8d66637 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..215c35d --- /dev/null +++ b/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7299175 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..13cf9f5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ImplementCollectionOperationsUsingSinglyLinkedLists.iml b/ImplementCollectionOperationsUsingSinglyLinkedLists.iml new file mode 100644 index 0000000..4eba30b --- /dev/null +++ b/ImplementCollectionOperationsUsingSinglyLinkedLists.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/com/xlf/icousll/InputData.kt b/src/main/kotlin/com/xlf/icousll/InputData.kt new file mode 100644 index 0000000..338c71a --- /dev/null +++ b/src/main/kotlin/com/xlf/icousll/InputData.kt @@ -0,0 +1,113 @@ +package com.xlf.icousll + +object InputData { + fun readList(): Link { + var read: String + do { + read = readlnOrNull()!! + } while (read.isBlank()) + // 将内容编译进入数组 + return Link(read.split(",")) + } + + /** + * 对两个自定义链表进行判等操作 + */ + fun combinedCalculation(firstLink: Link, secondLink: Link): Boolean { + // 判断两个链表的长度是否相等 + if (firstLink.size != secondLink.size) { + return false + } + // 判断两个链表的元素是否相等 + var temp1 = firstLink.head.next + var temp2 = secondLink.head.next + while (temp1 != null && temp2 != null) { + if (temp1.value != temp2.value) { + return false + } + temp1 = temp1.next + temp2 = temp2.next + } + return true + } + + /** + * 对两个自定义链表进行交操作,并且输出交后的 {x,x,x} 值 + */ + fun handOver(firstLink: Link, secondLink: Link): String { + // 判断两个链表的长度是否相等 + if (firstLink.size != secondLink.size) { + return "两个集合的长度不相等,无法进行交操作" + } + // 判断两个链表的元素是否相等 + var temp1 = firstLink.head.next + var temp2 = secondLink.head.next + var result = "{ " + while (temp1 != null && temp2 != null) { + if (temp1.value == temp2.value) { + result += temp1.value + "," + } + temp1 = temp1.next + temp2 = temp2.next + } + result = result.substring(0, result.length - 1) + result += " }" + return result + } + + /** + * 对两个自定义链表进行并操作,并且输出并后的 {x,x,x} 值 + */ + fun combine(firstLink: Link, secondLink: Link): String { + // 判断两个链表的长度是否相等 + if (firstLink.size != secondLink.size) { + return "两个集合的长度不相等,无法进行并操作" + } + // 判断两个链表的元素是否相等 + var temp1 = firstLink.head.next + var temp2 = secondLink.head.next + var result = "{ " + while (temp1 != null && temp2 != null) { + result += temp1.value + "," + temp1 = temp1.next + temp2 = temp2.next + } + result = result.substring(0, result.length - 1) + result += " }" + return result + + } + + /** + * 对两个自定义链表进行差操作,并且输出差后的 {x,x,x} 值 + */ + fun difference(firstLink: Link, secondLink: Link): String { + // 判断两个链表的长度是否相等 + if (firstLink.size != secondLink.size) { + return "两个集合的长度不相等,无法进行差操作" + } + // 判断两个链表的元素是否相等 + var temp1 = firstLink.head.next + var temp2 = secondLink.head.next + var result = "{ " + while (temp1 != null && temp2 != null) { + if (temp1.value != temp2.value) { + result += temp1.value + "," + } + temp1 = temp1.next + temp2 = temp2.next + } + result = result.substring(0, result.length - 1) + result += " }" + return result + } + + /** + * 释放链表 + */ + fun freeLinkedList(link: Link): Boolean { + link.head.next = null + link.size = 0 + return true + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/xlf/icousll/Link.kt b/src/main/kotlin/com/xlf/icousll/Link.kt new file mode 100644 index 0000000..0763860 --- /dev/null +++ b/src/main/kotlin/com/xlf/icousll/Link.kt @@ -0,0 +1,29 @@ +package com.xlf.icousll + +class Link() { + val head: LinkNode = LinkNode(null, null) + var size: Int = 0 + + /** + * 数组调用 + */ + constructor(split: List) : this() { + for (i in split) { + add(i) + } + } + + /** + * 添加元素 + * + * @param data + */ + fun add(data: R) { + var temp = head + while (temp.next != null) { + temp = temp.next!! + } + temp.next = LinkNode(data, null) + size++ + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/xlf/icousll/LinkNode.kt b/src/main/kotlin/com/xlf/icousll/LinkNode.kt new file mode 100644 index 0000000..fd65a31 --- /dev/null +++ b/src/main/kotlin/com/xlf/icousll/LinkNode.kt @@ -0,0 +1,6 @@ +package com.xlf.icousll + +data class LinkNode( + var value: R?, + var next: LinkNode? +) \ No newline at end of file diff --git a/src/main/kotlin/com/xlf/icousll/Main.kt b/src/main/kotlin/com/xlf/icousll/Main.kt new file mode 100644 index 0000000..12c489b --- /dev/null +++ b/src/main/kotlin/com/xlf/icousll/Main.kt @@ -0,0 +1,26 @@ +package com.xlf.icousll + +fun main() { + println("==============================") + println("题目:用单链表实现集合的操作") + println("作者:曾昶雯(筱锋xiao_lfeng)") + println("语言:kotlin-1.9") + println("==============================") + print("\n") + // 载入数据 + print("请输入第一个集合的元素,以英文逗号“,”分隔:") + val firstLink = InputData.readList() + print("请输入第二个集合的元素,以英文逗号“,”分隔:") + val secondLink = InputData.readList() + + // 对数据进行计算 + println("集合是否相等:${InputData.combinedCalculation(firstLink, secondLink)}") + println("集合的交运算:${InputData.handOver(firstLink, secondLink)}") + println("集合的并运算:${InputData.combine(firstLink, secondLink)}") + println("集合的差运算:${InputData.difference(firstLink, secondLink)}") + + // 释放链表 + println("\n正在释放链表:") + println("链表一:" + InputData.freeLinkedList(firstLink)) + println("链表二:" + InputData.freeLinkedList(secondLink)) +} \ No newline at end of file