-
Notifications
You must be signed in to change notification settings - Fork 42
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
Niv #19
base: master
Are you sure you want to change the base?
Niv #19
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,68 @@ | ||
class Queue | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@BUFFER_SIZE = 10 | ||
@store = [] | ||
@front = -1 | ||
@rear = -1 | ||
end | ||
|
||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == 0 && @rear == @BUFFER_SIZE - 1 | ||
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 could run into trouble if the Queue becomes full and the front is not at index 0. Consider if you had a buffer size of 10, You add 9 elements, remove one (so front is at index 1) and then try to add 7 more. What happens? |
||
raise Error, "Queue is Full" | ||
elsif @front == -1 | ||
@front = 0 | ||
@rear = 0 | ||
@store[@rear] = element | ||
elsif @rear == @store.length - 1 && @front != 0 | ||
@rear = 0 | ||
@store[@rear] = element | ||
else | ||
@rear += 1 | ||
@store[@rear] = element | ||
end | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
removed_data = @store[@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. Before dequeuing you should check to see if the queue is empty. |
||
@store[@front] = nil | ||
if (@front == @rear) | ||
@front = -1 | ||
@rear = -1 | ||
elsif @front === @BUFFER_SIZE - 1 | ||
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 kind of elsif statements work or you could do: |
||
@front = 0 | ||
else | ||
@front += 1 | ||
end | ||
return removed_data | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front] | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store.length | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == -1 | ||
return true | ||
end | ||
return false | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
display_store = [] | ||
if @front <= @rear | ||
display_store = @store[@front...@rear+1] | ||
else | ||
first_half = @store[0...@rear] | ||
second_half = @store[@[email protected]+1] | ||
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. I don't think this actually works. |
||
display_store.push(first_half) | ||
display_store.push(second_half) | ||
end | ||
return display_store.to_s | ||
end | ||
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.
Great use of a hash!