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
37 changes: 29 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
class Queue

MAX_SIZE = 20

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(MAX_SIZE)
@head = @tail = 0
@size = 0
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @size == MAX_SIZE
raise StandardError, "Queue already reached its max size."
end
@store[@tail] = element
@tail = (@tail + 1) % MAX_SIZE
@size += 1
end

def dequeue

Choose a reason for hiding this comment

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

What about if @size is 0?

raise NotImplementedError, "Not yet implemented"
result = front
@store[@head] = nil
@head = (@head + 1) % MAX_SIZE
@size -= 1
return result
end

def front
raise NotImplementedError, "Not yet implemented"
if empty?
raise StandardError, "Queue is empty."
end
return @store[@head]
end

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

def empty?
raise NotImplementedError, "Not yet implemented"
return @size == 0
end

def to_s

Choose a reason for hiding this comment

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

Well done!

return @store.to_s
if empty?
return ""
elsif @head < @tail
return @store[@head...@tail].to_s
else
return @store[@head..].concat(@store[0...@tail]).to_s
end
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 @@
require_relative './linked_list.rb'

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
@head = nil
end

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

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

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

def to_s
Expand Down
2 changes: 1 addition & 1 deletion test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
expect(q.empty?).must_equal true
end

it "returns the front element in the Queue" do
it "returns the element in the Queue" do
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand Down