Skip to content
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
50 changes: 42 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
class Queue

MAX_SIZE = 20

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(MAX_SIZE)
@front = -1
@back = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == 0 && @back == MAX_SIZE - 1 || @back == (@front - 1) % (MAX_SIZE - 1)

Choose a reason for hiding this comment

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

This mostly works but look at the 2nd OR condition here.

raise ArgumentError
elsif @front == -1
@front = 0
@back = 0
elsif @back == MAX_SIZE - 1 && @front != 0
@back = 0
else
@back += 1
end
@store[@back] = element
Comment on lines +12 to +22

Choose a reason for hiding this comment

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

Suggested change
if @front == 0 && @back == MAX_SIZE - 1 || @back == (@front - 1) % (MAX_SIZE - 1)
raise ArgumentError
elsif @front == -1
@front = 0
@back = 0
elsif @back == MAX_SIZE - 1 && @front != 0
@back = 0
else
@back += 1
end
@store[@back] = element
if @front == (@back + 1) % MAX_SIZE
raise ArgumentError, "Queue is full"
elsif @front == -1
@front = 0
end
@back = (@back + 1) % MAX_SIZE
@store[@back] = element

end

def dequeue
raise NotImplementedError, "Not yet implemented"
return nil if @store.empty?
raise ArgumentError if @front == -1

Choose a reason for hiding this comment

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

@front is only going to be -1 if the queue is empty.

element = @store[@front]
@store[@front] = nil

if @front == @back
@front = -1
@back = -1
elsif @front == MAX_SIZE - 1
@front = 0
else
@front += 1
end
return element
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.

👍

raise NotImplementedError, "Not yet implemented"
return 0 if @front == 1
return @front < @back ? @back - @front + 1 : MAX_SIZE - @front + @back
end

def empty?

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"
@front == -1
end

def to_s
return @store.to_s
result_array = []
20.times do |i|

Choose a reason for hiding this comment

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

Do you need the magic number 20.times loop? Could you instead start i at @front and keep incrementing until you hit the back?

index = (i + @front) % MAX_SIZE
if @store[index]
result_array << @store[index]
end
end
return result_array.to_s
end
end
12 changes: 7 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
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?

item = @store.remove_first
return item
end

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

def to_s
Expand Down