-
Notifications
You must be signed in to change notification settings - Fork 41
Fire-Blaine #28
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?
Fire-Blaine #28
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,69 @@ | ||
| class Queue | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new() | ||
| @max = 20 | ||
| @front = 0 | ||
| @back = 0 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @back == @front-1 || (@front == 0 && @back == @max - 1) | ||
| # account for full Queue | ||
| return raise ArgumentError.new("Queue is full") | ||
| elsif self.empty? | ||
| @front = 0 | ||
| @back = 0 | ||
| elsif @back == @max-1 | ||
| @back = 0 # if last then circles around | ||
| else | ||
| @back = @back + 1 # incr by 1 | ||
| end | ||
| @store[@back] = element | ||
|
|
||
| end | ||
|
|
||
| def dequeue | ||
|
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 self.empty? | ||
| raise ArgumentError.new("Queue's Empty") | ||
| end | ||
|
|
||
| value = @store[@front] | ||
| @store[@front] = nil | ||
|
|
||
| if @front == @max - 1 # circle | ||
| @front = 0 | ||
| else | ||
| @front = @front + 1 | ||
| end | ||
| return value | ||
| end | ||
|
|
||
| def 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. 👍 |
||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store[@front] | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return 0 if @front == -1 | ||
| if @front < @back | ||
| @back - @front + 1 | ||
| else | ||
| @max - (@back - @front) | ||
| end | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # return @front == -1 | ||
| return @store.all?{ |e| e.nil? } | ||
| end | ||
|
Comment on lines
55
to
58
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. Why not instead check to see if front and back are both 0?
Author
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. Honestly, didn't think of it. All I knew was I wanted to make sure the list was empty and I was getting errors because of nil so I googled for a fix. :-( |
||
|
|
||
| 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 | ||
| list = [] | ||
| index = @front | ||
| until index == @back + 1 | ||
| list << @store[index] | ||
| index = (index + 1) % @max | ||
| end | ||
| return list.to_s | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,23 @@ | ||
| 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 | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| # element into the store | ||
| @store.add_first(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| @store.remove_first | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| return @store.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.
But if the Queue is empty, the back and front are already set to zero.