Initial commit

This commit is contained in:
筱锋xiao_lfeng 2023-11-21 16:04:32 +08:00
commit 65431b0822
Signed by: XiaoLFeng
GPG Key ID: 6915780EF07491E5
13 changed files with 284 additions and 0 deletions

29
.gitignore vendored Normal file
View File

@ -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

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,5 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
</profile>
</component>

10
.idea/kotlinc.xml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.9" />
<option name="languageVersion" value="1.9" />
</component>
</project>

View File

@ -0,0 +1,23 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.20" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.20/kotlin-stdlib-jdk8-1.9.20.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.20/kotlin-stdlib-1.9.20.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.20/kotlin-stdlib-jdk7-1.9.20.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.20/kotlin-stdlib-jdk8-1.9.20-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.20/kotlin-stdlib-1.9.20-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.20/kotlin-stdlib-jdk7-1.9.20-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.20/kotlin-stdlib-jdk8-1.9.20-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.20/kotlin-stdlib-1.9.20-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.20/kotlin-stdlib-jdk7-1.9.20-sources.jar!/" />
</SOURCES>
</library>
</component>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="zulu-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ImplementCollectionOperationsUsingSinglyLinkedLists.iml" filepath="$PROJECT_DIR$/ImplementCollectionOperationsUsingSinglyLinkedLists.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

View File

@ -0,0 +1,113 @@
package com.xlf.icousll
object InputData {
fun readList(): Link<String> {
var read: String
do {
read = readlnOrNull()!!
} while (read.isBlank())
// 将内容编译进入数组
return Link(read.split(","))
}
/**
* 对两个自定义链表进行判等操作
*/
fun combinedCalculation(firstLink: Link<String>, secondLink: Link<String>): 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<String>, secondLink: Link<String>): 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<String>, secondLink: Link<String>): 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<String>, secondLink: Link<String>): 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<String>): Boolean {
link.head.next = null
link.size = 0
return true
}
}

View File

@ -0,0 +1,29 @@
package com.xlf.icousll
class Link<R>() {
val head: LinkNode<R> = LinkNode(null, null)
var size: Int = 0
/**
* 数组调用
*/
constructor(split: List<R>) : 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++
}
}

View File

@ -0,0 +1,6 @@
package com.xlf.icousll
data class LinkNode<R>(
var value: R?,
var next: LinkNode<R>?
)

View File

@ -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))
}