-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Lee #32
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 - Lee #32
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,64 @@ | ||||||||||||||||||||||||
| class Queue | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def initialize | ||||||||||||||||||||||||
| # @store = ... | ||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||
| @store = Array.new(20) | ||||||||||||||||||||||||
| @size = 20 | ||||||||||||||||||||||||
| @front = -1 | ||||||||||||||||||||||||
| @rear = -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 (@front == 0 && @rear == @size - 1) || | ||||||||||||||||||||||||
| (@rear == (@front - 1) % (@size - 1)) | ||||||||||||||||||||||||
| raise RuntimeError.new("Error: Queue is full") | ||||||||||||||||||||||||
| elsif @front == -1 # queue is empty | ||||||||||||||||||||||||
| @front = @rear = 0 | ||||||||||||||||||||||||
| @store[@rear] = element | ||||||||||||||||||||||||
| elsif @rear == @size - 1 && @front != 0 # rear needs to wrap around | ||||||||||||||||||||||||
| @rear = 0 | ||||||||||||||||||||||||
| @store[@rear] = element | ||||||||||||||||||||||||
| else | ||||||||||||||||||||||||
| @rear += 1 | ||||||||||||||||||||||||
| @store[@rear] = element | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
Comment on lines
+17
to
+23
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 could be simplified a bit.
Suggested change
|
||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def dequeue | ||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def front | ||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def size | ||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||
| if @front == -1 | ||||||||||||||||||||||||
| raise RuntimeError.new("Error: The queue is empty") | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| data = @store[@front] | ||||||||||||||||||||||||
| @store[@front] = nil # overwrite the element being deleted | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if @front == @rear # queue is now empty | ||||||||||||||||||||||||
| @front = @rear = -1 | ||||||||||||||||||||||||
| elsif @front == @size - 1 # front needs to wrap around | ||||||||||||||||||||||||
| @front = 0 | ||||||||||||||||||||||||
| else | ||||||||||||||||||||||||
| @front += 1 | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
Comment on lines
+36
to
+40
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. See above for a way to simplify this. |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def empty? | ||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||
| return @front == @rear | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def to_s | ||||||||||||||||||||||||
| return @store.to_s | ||||||||||||||||||||||||
| list = Array.new | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @store[@front...@size].each do |element| | ||||||||||||||||||||||||
| list.push(element) if element | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if @rear < @front # the queue has wrapped around | ||||||||||||||||||||||||
| @store[0..@rear].each do |element| | ||||||||||||||||||||||||
| list.push(element) if element | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return list.to_s | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| 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" | ||
| @store.add_first(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.remove_first | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.empty? | ||
| end | ||
|
|
||
| def top | ||
| @store.get_first | ||
| 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.
👍