Skip to content
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

Water - Beatrice #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@size = 10
@store = Array.new(@size)
@front = 0
@back = 0
end

def enqueue(element)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Not yet implemented"
# Check for full
if @front == (@back + 1) % @size
self.expand
end
Comment on lines +12 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This defeats the circular nature of the list, so that you can work with a fixed size, but it does let you resize the buffer.


@store[@back] = element
@back = (@back + 1) % @size
end

def dequeue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Not yet implemented"
if self.empty?
return nil
end

value = @store[@front]
@front = (@front + 1) % @size
return value
end

def front
raise NotImplementedError, "Not yet implemented"
return @store[@front]
end

def size

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size should be the number of elements in the queue not the size of the underlying buffer.

raise NotImplementedError, "Not yet implemented"
return @size
end

def empty?
raise NotImplementedError, "Not yet implemented"
return (@front == @back)
end

def expand
# Initialize new array
@new_store = []
new_back = 0

# Dequeue elements into new circular buffer
value = self.dequeue
while value
new_back += 1
@new_store.push(value)
value = self.dequeue
end

# Set new front back and size
@front = 0
@back = new_back
@size *= 2

# Pad new blank elements to fill out new size of buffer
@new_store += Array.new(@new_store.length)
@store = @new_store
end

def to_s
return @store.to_s
return @store[@front..(@back-1)].to_s

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if @back < @front

end
end
10 changes: 5 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class Stack

Choose a reason for hiding this comment

The 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"
return nil if @store.empty?
return @store.remove_first
end

def empty?
raise NotImplementedError, "Not yet implemented"
return @store.empty?
end

def to_s
Expand Down