-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
ksetoue
wants to merge
3
commits into
TheAlgorithms:master
Choose a base branch
from
ksetoue:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
/** | ||
* @returns the amount of itens inside the queue | ||
*/ | ||
fun getSize(): Int = itemsCount | ||
|
||
private fun isFull(): Boolean = itemsCount == maxCapacity | ||
|
||
private fun isEmpty(): Boolean = itemsCount == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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