-
Notifications
You must be signed in to change notification settings - Fork 42
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
Angela #7
base: master
Are you sure you want to change the base?
Angela #7
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,60 @@ | ||
QUEUE_SIZE = 20 | ||
|
||
class Queue | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new(QUEUE_SIZE) | ||
@front = @rear = -1 | ||
end | ||
|
||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
def enqueue(element) # add to rear | ||
if @front == -1 # Queue is empty | ||
@rear = 1 | ||
@front = 0 | ||
@store[@front] = element | ||
elsif @front == @rear | ||
raise Error, "Queue full" | ||
else | ||
new_rear = (@rear + 1) % QUEUE_SIZE | ||
@store[@rear] = element | ||
@rear = new_rear | ||
end | ||
return @store | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
def dequeue # remove from front | ||
removed = @store[@front] | ||
@store[@front] = nil | ||
self.size == 0 ? @front = -1 : @front += 1 | ||
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. What about the front wrapping around the array. Instead of |
||
return removed | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front] | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
size = 0 | ||
@store.each do |val| | ||
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 is less efficient than starting at |
||
if !val.nil? | ||
size += 1 | ||
end | ||
end | ||
return size | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
return true if self.size == 0 | ||
return false | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
def to_string | ||
formatted_array = [] | ||
@store.each do |x| | ||
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 assumes |
||
if !x.nil? | ||
formatted_array << x | ||
end | ||
end | ||
return formatted_array.to_s | ||
end | ||
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.
👍