-
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
Leaves - Mariya #38
base: master
Are you sure you want to change the base?
Leaves - Mariya #38
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,57 @@ | ||||||||||||||
class Queue | ||||||||||||||
|
||||||||||||||
def initialize | ||||||||||||||
# @store = ... | ||||||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||||||
@store = Array.new(10) | ||||||||||||||
@front = -1 | ||||||||||||||
@back = -1 | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
def enqueue(element) | ||||||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||||||
if @front == -1 && @back == -1 | ||||||||||||||
@front = 0 | ||||||||||||||
@back = 0 | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
if @front == @back && @store.compact.length > 0 | ||||||||||||||
front_array = @store.slice(0, @front) | ||||||||||||||
back_array = @store.slice(@back, @store.length - 1) | ||||||||||||||
@store = front_array + Array.new(10) + back_array | ||||||||||||||
@front = (@front + 10) % @store.length | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
@store[@back] = element | ||||||||||||||
@back = (@back + 1) % @store.length | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
def dequeue | ||||||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||||||
removed = @store[@front] | ||||||||||||||
@store[@front] = nil | ||||||||||||||
@front = (@front + 1) % @store.length | ||||||||||||||
return removed | ||||||||||||||
Comment on lines
+28
to
+31
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 should also check if the queue is now empty. |
||||||||||||||
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 == @back | ||||||||||||||
return true | ||||||||||||||
else | ||||||||||||||
return false | ||||||||||||||
end | ||||||||||||||
Comment on lines
+43
to
+47
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 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. 👍 |
||||||||||||||
return @store.to_s | ||||||||||||||
if @front >= @back | ||||||||||||||
return (@store[@front..-1] + @store[0...@back]).to_s | ||||||||||||||
else | ||||||||||||||
return @store[@front...@back].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.
This assumes that front is less than back