-
Notifications
You must be signed in to change notification settings - Fork 49
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
Kristy, Branches #29
base: master
Are you sure you want to change the base?
Kristy, Branches #29
Changes from all commits
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,31 +1,49 @@ | ||
class Queue | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new(100) | ||
@front = -1 | ||
@back = -1 | ||
end | ||
|
||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
end | ||
if @front == -1 && @back == -1 | ||
@front = 0 | ||
@back = 0 | ||
end | ||
|
||
if @front == @back | ||
#decide | ||
end | ||
Comment on lines
+15
to
+17
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 part works better as an elsif to raise an error if the queue is full. You're not doing anything here. |
||
|
||
@store[@back] = element | ||
@back = (@back + 1) % @store.length | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
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. You also need to check if the queue is empty before and after you dequeue an element. |
||
index = @front | ||
element = @store[index] | ||
|
||
@store[index] = nil | ||
|
||
@front = (@front + 1) % @store.length | ||
return element | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front] | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store.compact.length | ||
end | ||
Comment on lines
38
to
40
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 works, but it's quite expensive O(n) for both space and time and you lose some of the benefits of the circular buffer. |
||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
@store.compact.empty? ? true : false | ||
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. Why the conditional? Why not just Oh and like the previous method, this becomes very expensive. |
||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
return @store.compact.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. This mostly works, but if back < front, the order of materials in the queue won't be maintained. |
||
end | ||
end |
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.
👍