-
Notifications
You must be signed in to change notification settings - Fork 48
Branches - Amal #44
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?
Branches - Amal #44
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,35 @@ | ||||||
| class Queue | ||||||
|
|
||||||
| class Queue | ||||||
| def initialize | ||||||
| # @store = ... | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| @store = Array.new(10) | ||||||
| @front = @back = -1 | ||||||
| end | ||||||
|
|
||||||
| def enqueue(element) | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| if @front == -1 && @back == -1 | ||||||
| @front = 0 | ||||||
| @back = 1 | ||||||
| end | ||||||
| if @front == @back | ||||||
| raise ArgumenetError | ||||||
| end | ||||||
| @store[@back] = element | ||||||
| @back = (@back + 1) % @store.length | ||||||
| end | ||||||
|
|
||||||
| def dequeue | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| if @front == -1 | ||||||
| raise ArgumenetError | ||||||
|
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
|
||||||
| elsif @front == @back | ||||||
| @front = -1 | ||||||
| @back = -1 | ||||||
|
Comment on lines
+23
to
+25
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. The queue won't be empty until you remove an element and advance @front |
||||||
| else | ||||||
| first = @store[@front] | ||||||
| @store[@front] = nil | ||||||
| @front = (@front + 1) % @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. After this line is where you should check to see if the queue is empty. |
||||||
| end | ||||||
|
|
||||||
| return first | ||||||
| end | ||||||
|
|
||||||
| def front | ||||||
|
|
@@ -22,10 +41,10 @@ def size | |||||
| end | ||||||
|
|
||||||
| def empty? | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| return true if @front = -1 && @back - 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.
Suggested change
|
||||||
| end | ||||||
|
|
||||||
| def to_s | ||||||
| return @store.to_s | ||||||
| return @store[@front + 1...@back].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. |
||||||
| 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.
I suggest making this an elsif otherwise front and back will have an extra space between them due to the fact that the 1st if sets back at 1.