Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# create a link list example #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ repositories {

dependencies {
testCompile 'junit:junit:4.12'
testCompile 'com.carrotsearch:junit-benchmarks:0.7.2'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
49 changes: 49 additions & 0 deletions src/main/io/uuddlrlrba/ktalgs/datastructures/LinkedList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2017 Kotlin Algorithm Club
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.uuddlrlrba.ktalgs.datastructures

enum class LinkedType {
END, NODE
}

class LinkedListKotlin<T>: Cloneable{

private data class Node<T>(val item: T? = null, val tail: LinkedListKotlin<T>? = null)

private var type: LinkedType = LinkedType.END
private var node: Node<T>? = null

fun push(item: T) {
val tail = this.clone() as? LinkedListKotlin<T>
this.node = Node(item, tail)
type = LinkedType.NODE
}

fun pop(): T? {
val returnItem = this.node?.copy()?.item
val nextNode = this.node?.tail?.node
this.node = Node(nextNode?.item, nextNode?.tail)
return returnItem
}
}

70 changes: 70 additions & 0 deletions src/test/io/uuddlrlrba/ktalgs/datastructures/LinkedListTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2017 Kotlin Algorithm Club
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.uuddlrlrba.ktalgs.datastructures

import com.carrotsearch.junitbenchmarks.BenchmarkOptions
import com.carrotsearch.junitbenchmarks.BenchmarkRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test


class LinkedListTest {

private lateinit var linkedList: LinkedListKotlin<Int>

@Before
fun init() {
linkedList = LinkedListKotlin()
}

@get:Rule
public val benchmarkRule: BenchmarkRule = BenchmarkRule()

@Test
fun emptyTest() {
val test2 = linkedList.pop()
}


@Test
fun pushAndPopTest() {
val linkedList: LinkedListKotlin<Int> = LinkedListKotlin()
linkedList.push(1)
linkedList.push(2)
linkedList.push(3)
val test1 = linkedList.pop()
val test2 = linkedList.pop()
}

@Test
@BenchmarkOptions(concurrency = 1, warmupRounds = 0, benchmarkRounds = 1)
fun benchmarkSomeWork() {
for (i in 0..1000000) {
linkedList.push(i)
}
for (i in 0..1000000) {
linkedList.pop()
}
}
}