Skip to content

Instantly share code, notes, and snippets.

View arjrr's full-sized avatar

Arilson arjrr

View GitHub Profile
/** 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++
}
/** 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++
}
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)
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
}
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
}
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
}
}
class AdjacencyList<T> {
private val adjacencyMap = mutableMapOf<Vertex<T>, ArrayList<Edge<T>>>()
}
data class Edge<T>(val source: Vertex<T>, val destination: Vertex<T>, val weight: Double? = null)
data class Vertex<T>(val index: Int, val data: T)
fun main() {
val linkedList = LinkedList()
linkedList.push("Lorem")
linkedList.push("Ipsum")
}