Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue implementation #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ repositories {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'org.assertj:assertj-core:3.21.0'

testCompile group: 'junit', name: 'junit', version: '4.12'
}

Expand Down
74 changes: 74 additions & 0 deletions src/main/kotlin/datastructures/queues/Queue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package datastructures

const val DEFAULT_CAPACITY = 10

/**
* This is an implementation of a Queue
* that uses an array as primary structure
*
* A queue data structure functions the same as a real world queue. The elements
* that are added first are the first to be removed. New elements are added to
* the back/rear of the queue.
*/
class Queue (size: Int?) {

private val maxCapacity = size ?: DEFAULT_CAPACITY

private var itemsCount = 0

private val queue = IntArray(maxCapacity)

private var last = -1

private var first = 0

/**
* @param value is an integer that will be inserted into the queue
* @returns false if the queue is full and true if the item was inserted
*/
fun enqueue(value: Int): Boolean {
if (isFull()) return false

itemsCount++

last = (last + 1) % maxCapacity
queue[last] = value

return true
}

/**
* @returns the first item inserted into the queue, and null if it's empty
*/
fun dequeue(): Int? {
if (isEmpty()) {
return null
}

val itemToReturn = queue[first]
first = (first + 1) % maxCapacity

itemsCount--

return itemToReturn
}

/**
* @returns the first item inserted into the queue as string
*/
fun peekFirst(): String = queue[first].toString()

/**
* @returns the last item inserted into the queue as string
*/
fun peekLast(): String = queue[last].toString()
Comment on lines +57 to +64

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return the int directly should be better, also care about empty list case


/**
* @returns the amount of itens inside the queue
*/
fun getSize(): Int = itemsCount

private fun isFull(): Boolean = itemsCount == maxCapacity

private fun isEmpty(): Boolean = itemsCount == 0
}
59 changes: 59 additions & 0 deletions src/test/kotlin/datastructures/queues/QueueTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package datastructures.queues
import org.assertj.core.api.Assertions.assertThat
import datastructures.Queue

import org.junit.Test

internal class QueueTest {

@Test
fun `#enqueue must return true queue is not full`() {
val valueToInsert = 1

val newQueue = Queue(10)
val returnedValue = newQueue.enqueue(valueToInsert)

assertThat(returnedValue).isTrue()
}

@Test
fun `#enqueue must return false queue is full`() {
val valueToInsert = 20
val queueSize = 4
val newQueue = Queue(queueSize)

for(i in 0 until queueSize + 1) {
newQueue.enqueue(i)
}

val returnedValue = newQueue.enqueue(valueToInsert)

assertThat(returnedValue).isFalse()
}

@Test
fun `#dequeue must return null when queue is empty`() {
val queueSize = 2
val newQueue = Queue(queueSize)

val returnedValue = newQueue.dequeue()

assertThat(returnedValue).isNull()
}

@Test
fun `#dequeue must return a value when queue is not empty`() {
val valueToInsert = 20
val queueSize = 4
val newQueue = Queue(queueSize)

for(i in 0 until queueSize + 1) {
newQueue.enqueue(i)
}

val returnedValue = newQueue.dequeue()

assertThat(returnedValue).isNotNull()
assertThat(returnedValue).isEqualTo(0)
}
}