-
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
Nara #26
base: master
Are you sure you want to change the base?
Nara #26
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,55 @@ | ||
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" | ||
if @front == -1 | ||
@front = 0 | ||
@rear = 1 | ||
@store[@front] = element | ||
elsif @front == (@rear + 1) % QUEUE_SIZE | ||
raise Error, "Queue is full" | ||
else | ||
new_rear = (@rear + 1) % QUEUE_SIZE | ||
@store[@rear] = element | ||
@rear = new_rear | ||
end | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == @rear | ||
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. You first need to check to see if front and rear are both -1, if so, then the queue is empty, and you should raise an error. Otherwise if front == rear then the queue is full. |
||
@front = @rear = -1 | ||
raise Error, "Queue is empty" | ||
else | ||
new_front = (@front + 1) % QUEUE_SIZE | ||
old_front = @store[@front] | ||
@store[@front] = nil | ||
@front = new_front | ||
return old_front | ||
end | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @strore[@front] | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store.length | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
if @store[@front...@rear].length == 0 | ||
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 if the rear has wrapped around the buffer and is now less than the front? |
||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
return @store[@front...@rear].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.
👍