Initial commit
This commit is contained in:
commit
65431b0822
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal 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
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
5
.idea/inspectionProfiles/Project_Default.xml
Normal file
5
.idea/inspectionProfiles/Project_Default.xml
Normal 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
10
.idea/kotlinc.xml
Normal 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>
|
23
.idea/libraries/KotlinJavaRuntime.xml
Normal file
23
.idea/libraries/KotlinJavaRuntime.xml
Normal 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
6
.idea/misc.xml
Normal 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
8
.idea/modules.xml
Normal 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
6
.idea/vcs.xml
Normal 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>
|
15
ImplementCollectionOperationsUsingSinglyLinkedLists.iml
Normal file
15
ImplementCollectionOperationsUsingSinglyLinkedLists.iml
Normal 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>
|
113
src/main/kotlin/com/xlf/icousll/InputData.kt
Normal file
113
src/main/kotlin/com/xlf/icousll/InputData.kt
Normal 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
|
||||
}
|
||||
}
|
29
src/main/kotlin/com/xlf/icousll/Link.kt
Normal file
29
src/main/kotlin/com/xlf/icousll/Link.kt
Normal 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++
|
||||
}
|
||||
}
|
6
src/main/kotlin/com/xlf/icousll/LinkNode.kt
Normal file
6
src/main/kotlin/com/xlf/icousll/LinkNode.kt
Normal file
|
@ -0,0 +1,6 @@
|
|||
package com.xlf.icousll
|
||||
|
||||
data class LinkNode<R>(
|
||||
var value: R?,
|
||||
var next: LinkNode<R>?
|
||||
)
|
26
src/main/kotlin/com/xlf/icousll/Main.kt
Normal file
26
src/main/kotlin/com/xlf/icousll/Main.kt
Normal 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))
|
||||
}
|
Loading…
Reference in New Issue
Block a user