Skip to content

Commit 7deaaac

Browse files
Doubly linked list
1 parent cdc00f1 commit 7deaaac

File tree

4 files changed

+302
-2
lines changed

4 files changed

+302
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object Runner {
3131
println("------------------ Getting the last element ------------------")
3232
singlyLinkedList = getSinglyLinkedLit()
3333
val lastElement = singlyLinkedList.get(singlyLinkedList.getLength() - 1)
34-
println("lastElement = ${lastElement}")
34+
println("lastElement = $lastElement")
3535

3636
println("\n------------------ Setting new first element ------------------ ")
3737
singlyLinkedList = getSinglyLinkedLit()
@@ -60,7 +60,7 @@ object Runner {
6060
singlyLinkedList.push("first")
6161
singlyLinkedList.push("second")
6262

63-
return singlyLinkedList;
63+
return singlyLinkedList
6464
}
6565

6666
private fun printList(singlyLinkedList: SinglyLinkedList) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package pl.dmichalski.algorithms.data_structures._2_doubly_linked_list
2+
3+
class DoublyLinkedList {
4+
5+
private var head: Node?
6+
private var tail: Node?
7+
private var length: Int
8+
9+
init {
10+
this.head = null
11+
this.tail = null
12+
this.length = 0
13+
}
14+
15+
fun push(value: String) {
16+
val newNode = Node(value)
17+
if (this.head == null) {
18+
this.head = newNode
19+
this.tail = newNode
20+
} else {
21+
this.tail!!.setNext(newNode)
22+
newNode.setPrevious(this.tail)
23+
this.tail = newNode
24+
}
25+
length++
26+
}
27+
28+
fun pop() {
29+
if (length == 0) {
30+
return
31+
}
32+
33+
val nodeToRemove = tail
34+
35+
if (length == 1) {
36+
this.head = null
37+
this.tail = null
38+
} else {
39+
this.tail = nodeToRemove!!.getPrevious()
40+
this.tail!!.setNext(null)
41+
nodeToRemove.setPrevious(null)
42+
}
43+
44+
length--
45+
}
46+
47+
fun shift() {
48+
if (length == 0) {
49+
return
50+
}
51+
52+
val oldHead = head
53+
if (this.length == 1) {
54+
this.head = null
55+
this.tail = null
56+
}
57+
head = oldHead!!.getNext()
58+
this.head!!.setPrevious(null)
59+
oldHead.setNext(null)
60+
61+
length--
62+
}
63+
64+
fun unshift(value: String) {
65+
val newNode = Node(value)
66+
67+
if (this.head == null) {
68+
this.head = newNode
69+
this.tail = newNode
70+
} else {
71+
head!!.setPrevious(newNode)
72+
newNode.setNext(head)
73+
head = newNode
74+
}
75+
76+
length++
77+
}
78+
79+
fun get(index: Int): Node? {
80+
if (index < 0 || index >= length) {
81+
return null
82+
}
83+
84+
var currentIndex: Int?
85+
var current: Node?
86+
87+
if (index <= length / 2) {
88+
currentIndex = 0
89+
current = head
90+
91+
while (currentIndex != index) {
92+
current = current!!.getNext()
93+
currentIndex++
94+
}
95+
} else {
96+
currentIndex = length - 1
97+
current = tail
98+
99+
while (currentIndex != index) {
100+
current = current!!.getPrevious()
101+
currentIndex--
102+
}
103+
}
104+
105+
return current
106+
}
107+
108+
fun set(value: String, index: Int): Boolean {
109+
val node = get(index)
110+
return if (node != null) {
111+
node.setValue(value)
112+
true
113+
} else {
114+
false
115+
}
116+
}
117+
118+
fun insert(value: String, index: Int): Boolean {
119+
if (index < 0 || index > length) {
120+
return false
121+
}
122+
123+
if (index == 0) {
124+
this.unshift(value)
125+
return true
126+
}
127+
128+
if (index == length) {
129+
this.push(value)
130+
return true
131+
}
132+
133+
val newNode = Node(value)
134+
val previous = this.get(index - 1)
135+
val nextNode = previous!!.getNext()
136+
137+
previous.setNext(newNode)
138+
newNode.setPrevious(previous)
139+
140+
newNode.setNext(nextNode)
141+
newNode.setPrevious(newNode)
142+
length++
143+
return true
144+
}
145+
146+
fun remove(index: Int): Node? {
147+
if (index < 0 || index >= length) {
148+
return null
149+
}
150+
151+
if (index == 0) {
152+
val firstElement = head
153+
shift()
154+
return firstElement
155+
}
156+
157+
if (index == length - 1) {
158+
val lastElement = tail
159+
pop()
160+
return lastElement
161+
}
162+
163+
val elementToRemove = get(index)
164+
elementToRemove!!.getPrevious()!!.setNext(elementToRemove.getNext())
165+
elementToRemove.getNext()!!.setPrevious(elementToRemove.getPrevious())
166+
167+
elementToRemove.setPrevious(null)
168+
elementToRemove.setNext(null)
169+
170+
length--
171+
return elementToRemove
172+
}
173+
174+
fun getHead(): Node? {
175+
return head
176+
}
177+
178+
fun getTail(): Node? {
179+
return tail
180+
}
181+
182+
fun getLength(): Int {
183+
return length
184+
}
185+
186+
override fun toString(): String {
187+
return "Head: $head\n" +
188+
"Tail: $tail\n" +
189+
"Length: $length\n"
190+
}
191+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package pl.dmichalski.algorithms.data_structures._2_doubly_linked_list
2+
3+
class Node(private var value: String) {
4+
5+
private var previous: Node?
6+
private var next: Node?
7+
8+
init {
9+
this.previous = null
10+
this.next = null
11+
}
12+
13+
fun getValue(): String {
14+
return value
15+
}
16+
17+
fun setValue(value: String) {
18+
this.value = value
19+
}
20+
21+
fun getPrevious(): Node? {
22+
return previous
23+
}
24+
25+
fun setPrevious(previousNode: Node?) {
26+
previous = previousNode
27+
}
28+
29+
fun getNext(): Node? {
30+
return next
31+
}
32+
33+
fun setNext(nextNode: Node?) {
34+
next = nextNode
35+
}
36+
37+
override fun toString(): String {
38+
return "Node(value='$value', " +
39+
"previousValue=${previous?.getValue()}, " +
40+
"nextValue=${next?.getValue()})"
41+
}
42+
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package pl.dmichalski.algorithms.data_structures._2_doubly_linked_list
2+
3+
object Runner {
4+
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("------------------ Initial doubly linked list ------------------ ")
8+
var doublyLinkedList = getDoublyLinkedLit()
9+
printList(doublyLinkedList)
10+
11+
println("------------------ After adding third element to the end ------------------ ")
12+
doublyLinkedList = getDoublyLinkedLit()
13+
doublyLinkedList.push("third")
14+
printList(doublyLinkedList)
15+
16+
println("------------------ After removing last element ------------------ ")
17+
doublyLinkedList = getDoublyLinkedLit()
18+
doublyLinkedList.pop()
19+
printList(doublyLinkedList)
20+
21+
println("------------------ After removing first element ------------------ ")
22+
doublyLinkedList = getDoublyLinkedLit()
23+
doublyLinkedList.shift()
24+
printList(doublyLinkedList)
25+
26+
println("------------------ After adding element to the beginning ------------------ ")
27+
doublyLinkedList = getDoublyLinkedLit()
28+
doublyLinkedList.unshift("before first")
29+
printList(doublyLinkedList)
30+
31+
println("------------------ Getting the last element ------------------")
32+
doublyLinkedList = getDoublyLinkedLit()
33+
val lastElement = doublyLinkedList.get(doublyLinkedList.getLength() - 1)
34+
println("lastElement = ${lastElement}")
35+
36+
println("\n------------------ Setting new first element ------------------ ")
37+
doublyLinkedList = getDoublyLinkedLit()
38+
doublyLinkedList.set("new first", 0)
39+
printList(doublyLinkedList)
40+
41+
println("------------------ Insert element to the beginning ------------------ ")
42+
doublyLinkedList = getDoublyLinkedLit()
43+
doublyLinkedList.insert("before first", 0)
44+
printList(doublyLinkedList)
45+
46+
println("------------------ Removing first element ------------------ ")
47+
doublyLinkedList = getDoublyLinkedLit()
48+
doublyLinkedList.remove(0)
49+
printList(doublyLinkedList)
50+
}
51+
52+
private fun getDoublyLinkedLit(): DoublyLinkedList {
53+
val doublyLinkedList = DoublyLinkedList()
54+
55+
doublyLinkedList.push("first")
56+
doublyLinkedList.push("second")
57+
58+
return doublyLinkedList;
59+
}
60+
61+
private fun printList(doublyLinkedList: DoublyLinkedList) {
62+
println(doublyLinkedList)
63+
}
64+
65+
}

0 commit comments

Comments
 (0)