-
Notifications
You must be signed in to change notification settings - Fork 47
Branches - Emily Ball #39
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,31 +1,71 @@ | ||||||||||
| class Queue | ||||||||||
|
|
||||||||||
| attr_reader :front, :back, :store | ||||||||||
| def initialize | ||||||||||
| # @store = ... | ||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||
| @size = 20 | ||||||||||
| @store = Array.new(@size) | ||||||||||
| @front = -1 | ||||||||||
| @back = -1 | ||||||||||
| 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 empty? | ||||||||||
| @front = 0 | ||||||||||
| @back = 0 | ||||||||||
| elsif @front == @back | ||||||||||
| raise StandardError.new "Queue is out of room! Needs to be resized." | ||||||||||
| end | ||||||||||
|
|
||||||||||
| @store[@back] = element | ||||||||||
| @back = (@back + 1) % @store.length | ||||||||||
| 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" | ||||||||||
| return nil if empty? | ||||||||||
|
|
||||||||||
| removed = @store[@front] | ||||||||||
| @store[@front] = nil | ||||||||||
|
|
||||||||||
| if @back - 1 == @front | ||||||||||
| @front = -1 | ||||||||||
| @back = -1 | ||||||||||
| else | ||||||||||
| @front = (@front + 1) % @store.length | ||||||||||
| end | ||||||||||
| return removed | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def front | ||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||
| return nil if empty? | ||||||||||
| return @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" | ||||||||||
| return 0 if empty? | ||||||||||
| return @front - @back if @front > @back | ||||||||||
| return @back + (@store.length - @front) | ||||||||||
|
Comment on lines
+44
to
+45
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 have this reversed here.
Suggested change
|
||||||||||
| end | ||||||||||
|
|
||||||||||
| def empty? | ||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||
| @front == -1 && @back == -1 | ||||||||||
| end | ||||||||||
|
|
||||||||||
| 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. 👍 This works, but you could do it with just a single loop like |
||||||||||
| return @store.to_s | ||||||||||
| return [] if empty? | ||||||||||
| return @store.compact.to_s if @front < @back | ||||||||||
|
|
||||||||||
| output = [] | ||||||||||
| i = @front | ||||||||||
| until i > @store.length - 1 | ||||||||||
| output << @store[i] | ||||||||||
| i += 1 | ||||||||||
| end | ||||||||||
|
|
||||||||||
| i = 0 | ||||||||||
| until @store[i].nil? || i == @front | ||||||||||
| output << @store[i] | ||||||||||
| i += 1 | ||||||||||
| end | ||||||||||
|
|
||||||||||
| return output | ||||||||||
| end | ||||||||||
| end | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,24 @@ | ||
| require_relative '../lib/linked_list' | ||
|
|
||
| class Stack | ||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.add_last(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if empty? | ||
| return @store.remove_last | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store.empty? | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| @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.
👍