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
55 changes: 48 additions & 7 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,72 @@
class Queue
MAX_SIZE = 20

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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if (@front == 0 && @back == MAX_SIZE - 1) || (@back == (@front -1) % MAX_SIZE - 1)
raise Exception.new("Queue is full.")
elsif @store.empty?
@front = 0
@back = 0
elsif @back == (MAX_SIZE - 1) && @front != 0
@back = 0
else
@back += 1
end
Comment on lines +16 to +20

Choose a reason for hiding this comment

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

This could also be written as:

Suggested change
elsif @back == (MAX_SIZE - 1) && @front != 0
@back = 0
else
@back += 1
end
else
@back = (@back + 1) % MAX_SIZE
end


@store[@back] = element # in any non exception case, add element to queue
end

def dequeue
raise NotImplementedError, "Not yet implemented"
# raise StandardError.new if @front == @back

raise Exception.new("Queue is empty") if @store.empty?

num_to_dequeue = @store[@front] # save the num to dequeue
@store[@front] = nil # overwrite element to delete

if @front == @back # queue is now empty
@front = @back = -1
elsif @front + 1 == MAX_SIZE

Choose a reason for hiding this comment

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

Just a note that this could be updated similar to what I mentioned in enqueue.

@front = 0
else # otherwise, add to front
@front += 1
end

return num_to_dequeue
end

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

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

def empty?
raise NotImplementedError, "Not yet implemented"
return @front == @back
end

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 = Array.new

@store[@front...MAX_SIZE].each do |item|
list.push(item) if item
end

if @back < @front # the queue has wrapped around
@store[0..@back].each do |item|
list.push(item) if item
end
end

return list.to_s
end
end
15 changes: 9 additions & 6 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
class Stack
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 self.empty?

element = @store.remove_first

return element
end

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

def to_s
return @store.to_s
end
end
end