Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all tests passing #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
58 changes: 50 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,73 @@
class Queue
MAX_SIZE = 20

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

def enqueue(element)

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 @front == 0 && @back == MAX_SIZE - 1 || @back == @front - 1 % MAX_SIZE - 1
raise ArgumentError
elsif @store.empty?
@front = 0
@back = 0
elsif @back == MAX_SIZE - 1 && @front != 0
@back = 0
else
@back += 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 @store.empty?
raise ArgumentError
end

removed_num = @store[@front]
@store[@front] = nil

if @front == @back
@front = -1
@back = -1
elsif @front + 1 == MAX_SIZE
@front = 0
else
@front += 1
end
Comment on lines +36 to +40

Choose a reason for hiding this comment

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

Just be aware you can do things like:

Suggested change
elsif @front + 1 == MAX_SIZE
@front = 0
else
@front += 1
end
else
@front = (front + 1) % MAX_SIZE
end


return removed_num
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
return @store.to_s
list = Array.new

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

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

return list.to_s
end
end
8 changes: 4 additions & 4 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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"
@store.remove_first
end

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

def to_s