-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Nora #26
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?
Space - Nora #26
Changes from all commits
91ba8f4
ad5819b
dd20d13
9e49f58
426792a
d7e29ce
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,63 @@ | ||
| class Queue | ||
|
|
||
| # Using a circular buffer with an internal array starting at 20 elements, implement a Queue | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(20) | ||
| @size = 20 | ||
| @front = -1 | ||
| @back = -1 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # Adds the value to the back of the queue. | ||
| def enqueue(value) | ||
| if @front == -1 | ||
| @front = 0 | ||
| @back = 1 | ||
| @store[@front] = value | ||
| elsif @store.empty? | ||
| raise ArgumentError, "Queue is full" | ||
|
Comment on lines
+18
to
+19
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. Note about the readability here. You raise a Queue is full error if the array is empty? |
||
| else | ||
| @store[@back] = value | ||
| @back = (@back + 1) % @size | ||
| end | ||
| end | ||
|
|
||
| # removes and returns a value from the front of the queue | ||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == -1 | ||
| raise ArgumentError, "Queue is empty" | ||
| else | ||
| data = @store[@front] | ||
| @front = (@front + 1) % @size | ||
| if @store.empty? | ||
| @front = back = -1 | ||
| end | ||
| return data | ||
| end | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| end | ||
| # def front | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| # end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| end | ||
| # def size | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| # end | ||
|
|
||
| # returns true if the queue is empty and false otherwise | ||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == @back | ||
| end | ||
|
Comment on lines
49
to
51
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 reads better if |
||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| list = [] | ||
| current = @front | ||
|
|
||
| while current != @back | ||
| list << @store[current] | ||
| current = (current + 1) % @size | ||
| end | ||
| return list.to_s | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,24 @@ | ||
| 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" | ||
| @store = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # Adds the value to the top of the stack | ||
| def push(value) | ||
| @store.add_last(value) | ||
| end | ||
|
|
||
| # Removes and returns an element from the top of the stack | ||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if @store.empty? | ||
|
|
||
| @store.remove_last | ||
| end | ||
|
|
||
| # returns true if the stack is empty and false otherwise | ||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return true if @store.empty? | ||
| return false # if not 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.
Instead of this, you could simplify it with a hash. where the key is the close brace and the matching open brace is the value.