-
Notifications
You must be signed in to change notification settings - Fork 48
Kristina - Leaves #45
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?
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,16 +1,39 @@ | ||||||||||||||||
| class Queue | ||||||||||||||||
|
|
||||||||||||||||
| def initialize | ||||||||||||||||
| # @store = ... | ||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||
| @store = Array.new(100) | ||||||||||||||||
| @front = @back = -1 | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| def enqueue(element) | ||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||
| if @front == -1 && @back == -1 | ||||||||||||||||
| @front = @back = 0 | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| if @front == @back | ||||||||||||||||
| end | ||||||||||||||||
|
Comment on lines
+11
to
+14
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
|
||||||||||||||||
|
|
||||||||||||||||
| @store[@back] = element | ||||||||||||||||
| @back = (@back + 1) % @store.length | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| def dequeue | ||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||
| if @front == @back | ||||||||||||||||
| return "queue is empty" | ||||||||||||||||
|
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. It would be better to raise an error in this circumstance. |
||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| data = @store[@front] | ||||||||||||||||
| @store[@front] = nil | ||||||||||||||||
|
|
||||||||||||||||
| if @front == @back | ||||||||||||||||
|
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 think you need to advance front before comparing it to back. |
||||||||||||||||
| @front = @back = -1 | ||||||||||||||||
| elsif @front == @store.length - 1 | ||||||||||||||||
| @front = 0 | ||||||||||||||||
| else | ||||||||||||||||
| @front += 1 | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| return data | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| def front | ||||||||||||||||
|
|
@@ -22,10 +45,13 @@ def size | |||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| def empty? | ||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||
| if @front == @back | ||||||||||||||||
| return true | ||||||||||||||||
| end | ||||||||||||||||
|
Comment on lines
+48
to
+50
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 | ||||||||||||||||
| @store.delete(nil) | ||||||||||||||||
|
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 will shrink the internal array. It's both an expensive operation and it will also not maintain the order of elements in the Queue. |
||||||||||||||||
| return @store.to_s | ||||||||||||||||
| 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.
The method works, time and space complexity?