This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Adding a value after a particular node of the list. */ | |
fun insert(value: String, afterNode: Node) { | |
if (tail == afterNode) { | |
append(value) | |
return | |
} | |
afterNode.next = Node(value = value, next = afterNode.next) | |
size++ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Adding a value at the end of the list. */ | |
fun append(value: String) { | |
if (isEmpty()) { | |
push(value) | |
return | |
} | |
tail?.next = Node(value = value) | |
tail = tail?.next | |
size++ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val adjacencyListGraph = AdjacencyList<String>() | |
val brazil = adjacencyListGraph.createVertex("Brazil") | |
val germany = adjacencyListGraph.createVertex("Germany") | |
val thailand = adjacencyListGraph.createVertex("Thailand") | |
val unitedKingdom = adjacencyListGraph.createVertex("United Kingdom") | |
adjacencyListGraph.addDirectedEdge(brazil, germany, 1860.00) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdjacencyList<T> { | |
private val adjacencyMap = mutableMapOf<Vertex<T>, ArrayList<Edge<T>>>() | |
fun createVertex(data: T): Vertex<T> { | |
val vertex = Vertex(adjacencyMap.count(), data) | |
adjacencyMap[vertex] = arrayListOf() | |
return vertex | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdjacencyList<T> { | |
private val adjacencyMap = mutableMapOf<Vertex<T>, ArrayList<Edge<T>>>() | |
fun createVertex(data: T): Vertex<T> { | |
val vertex = Vertex(adjacencyMap.count(), data) | |
adjacencyMap[vertex] = arrayListOf() | |
return vertex | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdjacencyList<T> { | |
private val adjacencyMap = mutableMapOf<Vertex<T>, ArrayList<Edge<T>>>() | |
fun createVertex(data: T): Vertex<T> { | |
val vertex = Vertex(adjacencyMap.count(), data) | |
adjacencyMap[vertex] = arrayListOf() | |
return vertex | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdjacencyList<T> { | |
private val adjacencyMap = mutableMapOf<Vertex<T>, ArrayList<Edge<T>>>() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Edge<T>(val source: Vertex<T>, val destination: Vertex<T>, val weight: Double? = null) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Vertex<T>(val index: Int, val data: T) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val linkedList = LinkedList() | |
linkedList.push("Lorem") | |
linkedList.push("Ipsum") | |
} |