forked from AdaGold/stacks-queues
-
Notifications
You must be signed in to change notification settings - Fork 49
Angele Z. #41
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
Open
geli-gel
wants to merge
5
commits into
Ada-C12:master
Choose a base branch
from
geli-gel:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Angele Z. #41
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
042fb40
passing stack tests
geli-gel 08353ea
queue in progress
geli-gel 7ec7c33
completed dq and fixed test to expect correct results
geli-gel 5005b18
fixed stack .empty? method - would have always returned true
geli-gel 8a65359
trying to get the queue to add to the last spot and not move the back…
geli-gel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,83 @@ | ||
class Queue | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
# first in, first out, like a check-out line | ||
@store = Array.new(19) | ||
# pointers to @store nodes | ||
@front = @back = -1 | ||
end | ||
|
||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
# if empty then add element node to index 0 | ||
if @front == -1 && @back == -1 | ||
puts "setting front and back to 0" | ||
@front = 0 | ||
@back = 0 | ||
end | ||
# if queue is full | ||
if @front == @back && !@store[@front].nil? | ||
puts "adding #{element}, queue will be full" | ||
puts "(@front: #{@front}, @back: #{@back}" | ||
end | ||
puts "adding #{element} to @back located at index #{@back}" | ||
puts "@front: #{@front}" | ||
@store[@back] = element | ||
@back = (@back + 1) % @store.length | ||
puts "moved @back to #{@back}" | ||
return element | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
# if empty return nil | ||
if @front == -1 && @back == -1 | ||
return nil | ||
end | ||
# if queue is not empty return the first element in the list (at front) | ||
# and remove it from queue | ||
dequeued = @store[@front] | ||
@store[@front] = nil | ||
@front = (@front + 1) % @store.length | ||
# if it was the last one, set front and back to -1 | ||
if @front == @back && @store[@back].nil? | ||
puts "it was the last one, resetting queue" | ||
@front = @back = -1 | ||
end | ||
puts "removed #{dequeued} from queue, @front: #{@front}, @back: #{@back}" | ||
return dequeued | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
# from front to end | ||
if @front < @back | ||
size = @store[@front...@back].length | ||
# from back to end, then start to front | ||
else | ||
size = @store[@back..-1] + @store[0...@front] | ||
end | ||
return size | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
return @front == @back && @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. 👍 |
||
return @store.to_s | ||
# from front to end | ||
if @front < @back | ||
print_store = @store[@front...@back] | ||
# from front to end, then 0 to back | ||
else | ||
print_store = @store[@front..-1] + @store[0...@back] | ||
end | ||
puts "front and back:" | ||
puts @front | ||
puts @back | ||
puts "actual @store:" | ||
puts @store.to_s | ||
return print_store.to_s | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just noting dequeue and size are not implemented