-
Notifications
You must be signed in to change notification settings - Fork 44
Diana - Space #27
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?
Diana - Space #27
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,61 @@ | ||||||
| class Queue | ||||||
|
|
||||||
| def initialize | ||||||
| # @store = ... | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| attr_accessor :ring, :front, :back, :size | ||||||
|
|
||||||
| def initialize(size=20) | ||||||
| @size = size | ||||||
| @ring = Array.new(size) | ||||||
| @front = 0 | ||||||
| @back = 0 | ||||||
| end | ||||||
|
|
||||||
| def enqueue(element) | ||||||
|
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. 👍 |
||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| # if circular buffer is full, return empty array | ||||||
| return [] if (@back + 1) % @size == @front | ||||||
|
|
||||||
| # otherwise, add element to the back | ||||||
| @ring[@back] = element | ||||||
|
|
||||||
| # change @back to next free position, clockwise | ||||||
| @back = (@back + 1) % @size | ||||||
| end | ||||||
|
|
||||||
| def dequeue | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| return [] if @ring.empty? | ||||||
|
|
||||||
| dequeued_element = @ring[@front] | ||||||
| # dequeue by reassigning the front to the next position, clockwise | ||||||
| @front = (@front + 1) % @size | ||||||
|
|
||||||
| return dequeued_element | ||||||
| end | ||||||
|
|
||||||
| def front | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| @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.
Suggested change
|
||||||
| end | ||||||
|
|
||||||
| def size | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| @size | ||||||
| end | ||||||
|
Comment on lines
37
to
39
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. Size should be the number of elements in the queue. You can calculate it using the position of front and rear. |
||||||
|
|
||||||
| def empty? | ||||||
| raise NotImplementedError, "Not yet implemented" | ||||||
| @back == @front | ||||||
| end | ||||||
|
|
||||||
| def to_s | ||||||
| return @store.to_s | ||||||
| return [] if @ring.empty? | ||||||
|
|
||||||
| ring_output = [] | ||||||
| current_element = @front | ||||||
|
|
||||||
| while current_element != @back | ||||||
| # only get elements that are not nil | ||||||
| ring_output << @ring[current_element] | ||||||
|
|
||||||
| # shift to next element clockwise | ||||||
| current_element = (current_element + 1) % @size | ||||||
| end | ||||||
|
|
||||||
| return ring_output.to_s | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| class Stack | ||
|
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. 👍 |
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.add_first(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if self.empty? | ||
|
|
||
| @list.remove_first | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @list.empty? | ||
| end | ||
|
|
||
| def to_s | ||
|
|
||
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.
👍 , However you can dry up the case statement by using a hash instead. The has would have the open braces as the keys and the closing braces as the values.