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
56 changes: 47 additions & 9 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,69 @@
class Queue

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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @back == @front-1 || (@front == 0 && @back == @max - 1)
# account for full Queue
return raise ArgumentError.new("Queue is full")
elsif self.empty?
@front = 0
@back = 0
Comment on lines +14 to +16

Choose a reason for hiding this comment

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

But if the Queue is empty, the back and front are already set to zero.

elsif @back == @max-1
@back = 0 # if last then circles around
else
@back = @back + 1 # incr by 1
end
@store[@back] = element

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?
raise ArgumentError.new("Queue's Empty")
end

value = @store[@front]
@store[@front] = nil

if @front == @max - 1 # circle
@front = 0
else
@front = @front + 1
end
return value
end

def front

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 @store[@front]
end

def size
raise NotImplementedError, "Not yet implemented"
return 0 if @front == -1
if @front < @back
@back - @front + 1
else
@max - (@back - @front)
end
end

def empty?
raise NotImplementedError, "Not yet implemented"
# return @front == -1
return @store.all?{ |e| e.nil? }
end
Comment on lines 55 to 58

Choose a reason for hiding this comment

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

Why not instead check to see if front and back are both 0?

Copy link
Author

Choose a reason for hiding this comment

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

Honestly, didn't think of it. All I knew was I wanted to make sure the list was empty and I was getting errors because of nil so I googled for a fix. :-(


def to_s

Choose a reason for hiding this comment

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

👍

return @store.to_s
list = []
index = @front
until index == @back + 1
list << @store[index]
index = (index + 1) % @max
end
return list.to_s
end
end
end
14 changes: 9 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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
# raise NotImplementedError, "Not yet implemented"
end

def push(element)
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
# element into the store
@store.add_first(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
@store.remove_first
end

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

def to_s
Expand Down
34 changes: 32 additions & 2 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

it "adds something to an empty Queue" do

q = Queue.new
q.enqueue(10)
expect(q.to_s).must_equal "[10]"
Expand All @@ -22,6 +22,7 @@
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)

expect(q.to_s).must_equal "[10, 20, 30]"
end

Expand Down Expand Up @@ -50,7 +51,7 @@
expect(q.empty?).must_equal true
end

it "removes the right something (LIFO)" do
it "removes the right something (FIFO)" do

q = Queue.new
q.enqueue(5)
Expand Down Expand Up @@ -111,4 +112,33 @@

expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]')
end

it "calculates queues length/size" do
q = Queue.new
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.enqueue(1)
q.dequeue
q.dequeue
q.dequeue
q.dequeue
expect(q.size).must_equal(16)
end
end