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
33 changes: 26 additions & 7 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
class Queue

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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == -1 && @back == -1
@front = 0
@back = 1
end
if @front == @back
raise ArgumenetError
end
Comment on lines +12 to +15

Choose a reason for hiding this comment

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

I suggest making this an elsif otherwise front and back will have an extra space between them due to the fact that the 1st if sets back at 1.

Suggested change
end
if @front == @back
raise ArgumenetError
end
@back = 0
elsif @front == @back
raise ArgumentError, "Queue is full"
end

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

def dequeue
raise NotImplementedError, "Not yet implemented"
if @front == -1
raise ArgumenetError

Choose a reason for hiding this comment

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

Suggested change
raise ArgumenetError
raise ArgumentError, "Queue is Empty"

elsif @front == @back
@front = -1
@back = -1
Comment on lines +23 to +25

Choose a reason for hiding this comment

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

The queue won't be empty until you remove an element and advance @front

else
first = @store[@front]
@store[@front] = nil
@front = (@front + 1) % @store.length

Choose a reason for hiding this comment

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

After this line is where you should check to see if the queue is empty.

end

return first
end

def front
Expand All @@ -22,10 +41,10 @@ def size
end

def empty?
raise NotImplementedError, "Not yet implemented"
return true if @front = -1 && @back - 1

Choose a reason for hiding this comment

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

Suggested change
return true if @front = -1 && @back - 1
return @front = -1 && @back == - 1

end

def to_s
return @store.to_s
return @store[@front + 1...@back].to_s

Choose a reason for hiding this comment

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

This doesn't work, and assumes that @front < @back.

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 @@
require_relative './linked_list.rb'
class Stack
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new()
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@store.add_last(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
@store.remove_last()
end

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

def to_s
Expand Down
19 changes: 9 additions & 10 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
end

it "adds something to an empty Queue" do
skip
q = Queue.new
q.enqueue(10)
expect(q.to_s).must_equal "[10]"
end

it "adds multiple somethings to a Queue" do
skip
q = Queue.new
q.enqueue(10)
q.enqueue(20)
Expand All @@ -27,23 +25,24 @@
end

it "starts the size of a Queue at 0" do
skip

q = Queue.new
q.empty?.must_equal true
end

it "a Queue is empty after removing all the elements" do
skip

q = Queue.new
q.enqueue(5)
q.enqueue(6)

expect( expect(q.dequeue) ).must_equal 5
expect( expect(q.dequeue) ).must_equal 6
expect(q.empty?).must_equal true
end

it "removes something from the Queue" do
skip

q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -52,7 +51,7 @@
end

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

q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -63,7 +62,7 @@
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip

q = Queue.new
q.empty?.must_equal true
q.enqueue(-1)
Expand All @@ -75,7 +74,7 @@
end

it "returns the front element in the Queue" do
skip

q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand All @@ -84,6 +83,7 @@
expect(q.dequeue).must_equal 22
end
it "works for a large Queue" do

q = Queue.new
q.enqueue(10)
q.enqueue(20)
Expand All @@ -102,7 +102,6 @@
q.enqueue(130)
q.enqueue(140)
q.enqueue(150)
q.enqueue(150)
q.enqueue(160)
q.enqueue(170)
q.enqueue(180)
Expand All @@ -111,6 +110,6 @@
q.enqueue(210)
q.dequeue

expect(q.to_s).must_equal('[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]')
expect(q.to_s).must_equal('[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,210]')
end
end
5 changes: 0 additions & 5 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
end

it "pushes something onto a empty Stack" do
skip
s = Stack.new
s.push(10)
s.to_s.must_equal "[10]"
end

it "pushes multiple somethings onto a Stack" do
skip
s = Stack.new
s.push(10)
s.push(20)
Expand All @@ -26,13 +24,11 @@
end

it "starts the stack empty" do
skip
s = Stack.new
s.empty?.must_equal true
end

it "removes something from the stack" do
skip
s = Stack.new
s.push(5)
removed = s.pop
Expand All @@ -41,7 +37,6 @@
end

it "removes the right something (LIFO)" do
skip
s = Stack.new
s.push(5)
s.push(3)
Expand Down