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

Water - Mackenzie #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
57 changes: 50 additions & 7 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,74 @@
class Queue

MAX_SIZE = 20

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"

@store = Array.new(MAX_SIZE)
@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"
# raise NotImplementedError, "Not yet implemented"

if (@front == 0 && @back == MAX_SIZE - 1) || (@back == (@front - 1) % MAX_SIZE - 1)
raise ArgumentError.new, "Queue is full"
elsif @front == -1 && @back == -1
@front = 0
@back = 0
elsif @back == MAX_SIZE
@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"
# raise NotImplementedError, "Not yet implemented"

if @store.empty?
raise ArgumentError.new, "Queue is empty"
end

data = @store[@front]
@store[@front] = nil


if (@front == @back)
@front == -1
@back == -1
elsif (@front == MAX_SIZE - 1)
@front = 0
else
@front += 1
end

return data

end

def front

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"
# raise NotImplementedError, "Not yet implemented"
return @store[@front]
end

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

def empty?

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"
# raise NotImplementedError, "Not yet implemented"
return @store.compact.length == 0 ? true : false
end

def to_s

Choose a reason for hiding this comment

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

Just note that this will return the elements in the order they are in in the internal array, not in the queue order (i.e. the 1st element in the queue could be at index 3 and the last element of the queue at index 0.

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

item = @store.remove_first

return item
end

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

def to_s
Expand Down