Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cdc00f1

Browse files
Singly linked list
1 parent 759c4d3 commit cdc00f1

File tree

3 files changed

+160
-4
lines changed

3 files changed

+160
-4
lines changed

‎src/main/kotlin/pl/dmichalski/algorithms/data_structures/_1_singly_linked_list/Node.kt‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package pl.dmichalski.algorithms.data_structures._1_singly_linked_list
22

3-
class Node(private val value: String) {
3+
class Node(private var value: String) {
44

55
private var next: Node?
66

@@ -12,6 +12,10 @@ class Node(private val value: String) {
1212
return value
1313
}
1414

15+
fun setValue(value: String) {
16+
this.value = value
17+
}
18+
1519
fun getNext(): Node? {
1620
return next
1721
}
@@ -20,4 +24,9 @@ class Node(private val value: String) {
2024
next = nextNode
2125
}
2226

27+
override fun toString(): String {
28+
return "Node(value='$value', next=$next)"
29+
}
30+
31+
2332
}

‎src/main/kotlin/pl/dmichalski/algorithms/data_structures/_1_singly_linked_list/Runner.kt‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,67 @@ object Runner {
44

55
@JvmStatic
66
fun main(args: Array<String>) {
7-
val singlyLinkedList = SinglyLinkedList()
7+
println("------------------ Initial linked list ------------------ ")
8+
var singlyLinkedList = getSinglyLinkedLit()
9+
printList(singlyLinkedList)
810

9-
singlyLinkedList.push("first")
10-
singlyLinkedList.push("second")
11+
println("------------------ After adding third element to the end ------------------ ")
12+
singlyLinkedList= getSinglyLinkedLit()
1113
singlyLinkedList.push("third")
14+
printList(singlyLinkedList)
1215

16+
println("------------------ After removing last element ------------------ ")
17+
singlyLinkedList = getSinglyLinkedLit()
1318
singlyLinkedList.pop()
19+
printList(singlyLinkedList)
1420

21+
println("------------------ After removing first element ------------------ ")
22+
singlyLinkedList = getSinglyLinkedLit()
1523
singlyLinkedList.shift()
24+
printList(singlyLinkedList)
1625

26+
println("------------------ After adding element to the beginning ------------------ ")
27+
singlyLinkedList = getSinglyLinkedLit()
1728
singlyLinkedList.unshift("before first")
29+
printList(singlyLinkedList)
30+
31+
println("------------------ Getting the last element ------------------")
32+
singlyLinkedList = getSinglyLinkedLit()
33+
val lastElement = singlyLinkedList.get(singlyLinkedList.getLength() - 1)
34+
println("lastElement = ${lastElement}")
35+
36+
println("\n------------------ Setting new first element ------------------ ")
37+
singlyLinkedList = getSinglyLinkedLit()
38+
singlyLinkedList.set("new first", 0)
39+
printList(singlyLinkedList)
40+
41+
println("------------------ Insert element to the beginning ------------------ ")
42+
singlyLinkedList = getSinglyLinkedLit()
43+
singlyLinkedList.insert("before first", 0)
44+
printList(singlyLinkedList)
45+
46+
println("------------------ Removing first element ------------------ ")
47+
singlyLinkedList = getSinglyLinkedLit()
48+
singlyLinkedList.remove(0)
49+
printList(singlyLinkedList)
50+
51+
println("------------------ Revering the list ------------------ ")
52+
singlyLinkedList = getSinglyLinkedLit()
53+
singlyLinkedList.reverse()
54+
printList(singlyLinkedList)
55+
}
56+
57+
private fun getSinglyLinkedLit(): SinglyLinkedList {
58+
val singlyLinkedList = SinglyLinkedList()
59+
60+
singlyLinkedList.push("first")
61+
singlyLinkedList.push("second")
62+
63+
return singlyLinkedList;
64+
}
65+
66+
private fun printList(singlyLinkedList: SinglyLinkedList) {
67+
println(singlyLinkedList)
1868
}
1969

2070
}

‎src/main/kotlin/pl/dmichalski/algorithms/data_structures/_1_singly_linked_list/SinglyLinkedList.kt‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,98 @@ class SinglyLinkedList {
7272
length++
7373
}
7474

75+
fun get(index: Int): Node? {
76+
if (index < 0 || index >= length) {
77+
return null
78+
}
79+
80+
var currentIndex = 0
81+
var current = head
82+
83+
while (currentIndex != index) {
84+
current = current!!.getNext()
85+
currentIndex++
86+
}
87+
88+
return current
89+
}
90+
91+
fun set(value: String, index: Int): Boolean {
92+
val node = get(index)
93+
return if (node != null) {
94+
node.setValue(value)
95+
true
96+
} else {
97+
false
98+
}
99+
}
100+
101+
fun insert(value: String, index: Int): Boolean {
102+
if (index < 0 || index > length) {
103+
return false
104+
}
105+
106+
if (index == 0) {
107+
this.unshift(value)
108+
return true
109+
}
110+
111+
if (index == length) {
112+
this.push(value)
113+
return true
114+
}
115+
116+
val previous = this.get(index - 1)
117+
118+
val newNode = Node(value)
119+
newNode.setNext(previous!!.getNext())
120+
previous.setNext(newNode)
121+
length++
122+
return true
123+
}
124+
125+
fun remove(index: Int): Node? {
126+
if (index < 0 || index >= length) {
127+
return null
128+
}
129+
130+
if (index == 0) {
131+
val firstElement = head
132+
shift()
133+
return firstElement
134+
}
135+
136+
if (index == length - 1) {
137+
val lastElement = tail
138+
pop()
139+
return lastElement
140+
}
141+
142+
val previous = get(index - 1)
143+
val elementToRemove = previous!!.getNext()
144+
previous.setNext(elementToRemove!!.getNext())
145+
length--
146+
return elementToRemove
147+
}
148+
149+
fun reverse(): SinglyLinkedList {
150+
var currentNode = this.head
151+
this.head = this.tail
152+
this.tail = currentNode
153+
154+
var previous: Node? = null
155+
var nextNode: Node?
156+
157+
for (i in 0 until length) {
158+
nextNode = currentNode?.getNext()
159+
currentNode!!.setNext(previous)
160+
previous = currentNode
161+
currentNode = nextNode
162+
}
163+
164+
return this
165+
}
166+
75167
fun getHead(): Node? {
76168
return head
77169
}
@@ -84,4 +176,9 @@ class SinglyLinkedList {
84176
return length
85177
}
86178

179+
override fun toString(): String {
180+
return "Head: $head\n" +
181+
"Tail: $tail\n" +
182+
"Length: $length\n"
183+
}
87184
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /