-
Notifications
You must be signed in to change notification settings - Fork 49
Julia Kingrey #28
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
base: master
Are you sure you want to change the base?
Julia Kingrey #28
Changes from all commits
37a1e53
c0d5a7a
7fcc253
b5c0ba6
087be06
2ce0c16
aad8908
b707d71
2ff9da0
71de3f2
307f7fe
ac55be3
c1aab68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,28 +1,77 @@ | ||||||
class Queue | ||||||
|
||||||
def initialize | ||||||
# @store = ... | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
|
||||||
# # assume fixed array length is 10 | ||||||
@store = Array.new(10) | ||||||
|
||||||
# initialize front and back both at the same place | ||||||
@front = -1 | ||||||
@back = -1 | ||||||
|
||||||
# default status is empty | ||||||
@is_empty = true | ||||||
end | ||||||
|
||||||
def enqueue(element) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
raise NotImplementedError, "Not yet implemented" | ||||||
# implementing circular buffer to keep track of array's front and back indices (so insertions/deletions will be O of 1 -- would be O of n with if we used shovel/push) | ||||||
|
||||||
# if queue has space, move @back to the next free position, i.e. next position clockwise | ||||||
|
||||||
@back = (@back + 1) % size | ||||||
|
||||||
@store[@back] = element | ||||||
|
||||||
# resize queue if full: in this implementation, queue resizes at every multiple of 10 and reorders with front at index 0 | ||||||
if @front == @back && !@is_empty | ||||||
# add the new spaces after the 'back' of the queue | ||||||
new_queue_a = @store[@front+1..-1] | ||||||
new_queue_b = @store[0..@back-1] | ||||||
new_queue_c = Array.new(10) | ||||||
@back = @store.length - 1 | ||||||
@store = new_queue_a + new_queue_b + new_queue_c | ||||||
@store[@back] = element | ||||||
@front = -1 | ||||||
|
||||||
end | ||||||
|
||||||
# if queue is empty, change to not empty | ||||||
if @is_empty | ||||||
@is_empty = false | ||||||
end | ||||||
|
||||||
end | ||||||
|
||||||
def dequeue | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
raise NotImplementedError, "Not yet implemented" | ||||||
# increase at the front | ||||||
# check that the queue is empty when you reach the end | ||||||
|
||||||
@front = (@front + 1) % size | ||||||
|
||||||
# update is_empty for empty queue | ||||||
if @front == @back | ||||||
@is_empty = true | ||||||
end | ||||||
|
||||||
removed = @store[@front] | ||||||
|
||||||
# initially used delete_at(@front) but reassigning value to nil preserves fixed array size | ||||||
@store[@front] = nil | ||||||
|
||||||
# return the element that was removed | ||||||
return removed | ||||||
end | ||||||
|
||||||
def front | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
@front | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
end | ||||||
|
||||||
def size | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
return @store.length | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gets you the size of the internal array, not the amount of elements stored in the queue. |
||||||
end | ||||||
|
||||||
def empty? | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
return @is_empty | ||||||
end | ||||||
|
||||||
def to_s | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs to be updated. |
||||||
|
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.
This doesn't quite work as it only counts the number of the characters, not if they're matched.