Skip to content

Commit d4deae7

Browse files
Stack and Queue
1 parent 7deaaac commit d4deae7

File tree

6 files changed

+237
-0
lines changed

6 files changed

+237
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package pl.dmichalski.algorithms.data_structures._3_stack
2+
3+
class Node(private var value: String) {
4+
5+
private var next: Node?
6+
7+
init {
8+
this.next = null
9+
}
10+
11+
fun getValue(): String {
12+
return value
13+
}
14+
15+
fun setValue(value: String) {
16+
this.value = value
17+
}
18+
19+
fun getNext(): Node? {
20+
return next
21+
}
22+
23+
fun setNext(nextNode: Node?) {
24+
next = nextNode
25+
}
26+
27+
override fun toString(): String {
28+
return "Node(value='$value', next=$next)"
29+
}
30+
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package pl.dmichalski.algorithms.data_structures._3_stack
2+
3+
object Runner {
4+
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("------------------ Initial stack ------------------ ")
8+
var stack = getStack()
9+
printStack(stack)
10+
11+
println("------------------ After adding an element to the stack ------------------ ")
12+
stack = getStack()
13+
stack.push("before top")
14+
printStack(stack)
15+
16+
println("------------------ After removing element from the stack ------------------ ")
17+
stack = getStack()
18+
stack.pop()
19+
printStack(stack)
20+
}
21+
22+
private fun getStack(): Stack {
23+
val stack = Stack()
24+
25+
stack.push("bottom")
26+
stack.push("top")
27+
28+
return stack
29+
}
30+
31+
private fun printStack(stack: Stack) {
32+
println(stack)
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package pl.dmichalski.algorithms.data_structures._3_stack
2+
3+
import pl.dmichalski.algorithms.data_structures._1_singly_linked_list.Node
4+
5+
class Stack {
6+
7+
private var first: Node?
8+
private var last: Node?
9+
private var size: Int
10+
11+
init {
12+
this.first = null
13+
this.last = null
14+
this.size = 0
15+
}
16+
17+
fun push(value: String): Int {
18+
val newNode = Node(value)
19+
if (this.first == null) {
20+
this.first = newNode
21+
this.last = newNode
22+
} else {
23+
val temp = this.first
24+
this.first = newNode
25+
this.last
26+
this.first!!.setNext(temp)
27+
}
28+
return ++this.size
29+
}
30+
31+
fun pop(): String? {
32+
if (this.size == 0) {
33+
return null
34+
}
35+
36+
val temp = this.first
37+
38+
if (this.first == this.last) {
39+
this.last = null
40+
}
41+
this.first = this.first!!.getNext()
42+
this.size--
43+
return temp!!.getValue()
44+
}
45+
46+
override fun toString(): String {
47+
return "First: $first\n" +
48+
"Last: $last\n" +
49+
"Size: $size\n"
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package pl.dmichalski.algorithms.data_structures._4_queue
2+
3+
class Node(private var value: String) {
4+
5+
private var next: Node?
6+
7+
init {
8+
this.next = null
9+
}
10+
11+
fun getValue(): String {
12+
return value
13+
}
14+
15+
fun setValue(value: String) {
16+
this.value = value
17+
}
18+
19+
fun getNext(): Node? {
20+
return next
21+
}
22+
23+
fun setNext(nextNode: Node?) {
24+
next = nextNode
25+
}
26+
27+
override fun toString(): String {
28+
return "Node(value='$value', next=$next)"
29+
}
30+
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package pl.dmichalski.algorithms.data_structures._4_queue
2+
3+
import pl.dmichalski.algorithms.data_structures._1_singly_linked_list.Node
4+
5+
class Queue {
6+
7+
private var first: Node?
8+
private var last: Node?
9+
private var size: Int
10+
11+
init {
12+
this.first = null
13+
this.last = null
14+
this.size = 0
15+
}
16+
17+
fun enqueue(value: String): Int {
18+
val newNode = Node(value)
19+
if (this.first == null) {
20+
this.first = newNode
21+
this.last = newNode
22+
} else {
23+
this.last!!.setNext(newNode)
24+
this.last = newNode
25+
}
26+
return ++this.size
27+
}
28+
29+
fun dequeue(): String? {
30+
if (this.size == 0) {
31+
return null
32+
}
33+
34+
val temp = this.first
35+
36+
if (this.first == this.last) {
37+
this.last = null
38+
}
39+
this.first = this.first!!.getNext()
40+
this.size--
41+
return temp!!.getValue()
42+
}
43+
44+
override fun toString(): String {
45+
return "First: $first\n" +
46+
"Last: $last\n" +
47+
"Size: $size\n"
48+
}
49+
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package pl.dmichalski.algorithms.data_structures._4_queue
2+
3+
object Runner {
4+
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("------------------ Initial queue ------------------ ")
8+
var queue = getQueue()
9+
printQueue(queue)
10+
11+
println("------------------ After adding an element to the queue ------------------ ")
12+
queue = getQueue()
13+
queue.enqueue("after bottom")
14+
printQueue(queue)
15+
16+
println("------------------ After removing element from the queue ------------------ ")
17+
queue = getQueue()
18+
queue.dequeue()
19+
printQueue(queue)
20+
}
21+
22+
private fun getQueue(): Queue {
23+
val queue = Queue()
24+
25+
queue.enqueue("top")
26+
queue.enqueue("bottom")
27+
28+
return queue
29+
}
30+
31+
private fun printQueue(queue: Queue) {
32+
println(queue)
33+
}
34+
35+
}

0 commit comments

Comments
 (0)