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
40 changes: 32 additions & 8 deletions lib/queue.rb
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 #Q is empty
@rear = 1
@front = 0
@store[@front] = element
elsif @front == @rear
raise ArgumentError, "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 == -1
raise ArgumentError, "Queue is empty"
else
dequeued = @store[@front]
@store[@front] = nil
@front = (@front + 1) % QUEUE_SIZE

Choose a reason for hiding this comment

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

You should also check to see if the queue is now empty.

return dequeued
end
end

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

def size
raise NotImplementedError, "Not yet implemented"
return @store.compact.length

Choose a reason for hiding this comment

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

This is a bit expensive. You could also calculate the size based on the locations of front and rear.

end

def empty?
raise NotImplementedError, "Not yet implemented"
queue = @store.compact

Choose a reason for hiding this comment

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

This is a pretty expensive way to see if the queue is empty.

Why not just return @front == -1?

if queue.length == 0
return true
else
return false
end
end

def to_s
return @store.to_s
return @store.compact.to_s
end
end
36 changes: 30 additions & 6 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
class Stack
STACK_SIZE = 20

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

def push(element)
raise NotImplementedError, "Not yet implemented"
if @front == -1

Choose a reason for hiding this comment

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

You don't really need a circular buffer for a Stack as you only add or remove on one end (the top). Instead I'd asked you to use a Linked List. So this is a little weird.

@rear = 1
@front = 0
@store[@front] = element
elsif @front == @rear
raise ArgumentError, "Stack is full"
else
new_rear = (@rear + 1) % STACK_SIZE
@store[@rear] = element
@rear = new_rear
end
end

def pop
raise NotImplementedError, "Not yet implemented"
if @front == -1
raise ArgumentError, "Stack is empty"
else
popped = @store[@rear - 1]
@store[@rear - 1] = nil
@rear = (@rear - 1) % STACK_SIZE
return popped
end
end

def empty?
raise NotImplementedError, "Not yet implemented"
stack = @store.compact
if stack.length == 0
return true
else
return false
end
end

def to_s
return @store.to_s
return @store.compact.to_s
end
end
7 changes: 0 additions & 7 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)
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,13 +25,11 @@
end

it "starts the size of a Queue at 0" do
skip
q = Queue.new
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 @@ -42,7 +38,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -53,7 +48,6 @@
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 @@ -65,7 +59,6 @@
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand Down
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