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

Fire - Noor #23

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
46 changes: 38 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
class Queue
Array_Length = 20

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(Array_Length)
@front = @back = 0
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.nil?
@front = @back = 0
elsif (@front - @back == 1) || (@back - @front == Array_Length - 1)
raise ArgumentError, 'Queue is full'
end

@store[@back] = element
@back = (@back + 1) % Array_Length
end

def dequeue

Choose a reason for hiding this comment

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

👍 , very compact, nicely done!

raise NotImplementedError, "Not yet implemented"
if @front == @back || @front.nil?
raise ArgumentError, 'Queue is empty'
end

temp = @store[@front]
@front = (@front + 1) % Array_Length
return temp
end

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

def size

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"
return 0 if @front == -1
if @front < @back
@back - @front + 1
else
Array_Length - @front + @back
end
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"
if @front.nil? || @front == @back
return true
else
return false
end
end

def to_s
return @store.to_s
results = []
if @back >= @front
results = @store[@front...@back]
else
results = @store[@front...Array_Length] + (@store[0...@back])
end
return results.to_s
end

end
11 changes: 6 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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 @store.empty?
@store.remove_first
end

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

def to_s
Expand Down