Skip to content
Open

vi #42

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
34 changes: 30 additions & 4 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
class Queue

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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
#Circular buffer method
#Check if queue is empty
if @front == -1 && @back == -1
@front = 0
@back = 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're going to advance @back one step later in this method better to set it to 0

end

#Check if queue is full
if ((@back + 1) % @store.length) == @front
raise ArgumentError.new("Queue is full!")
end

@store[@back] = element
#Make it wrap around when it reaches the end & use modulo to not go over the end
@back = (@back + 1) % @store.length
end

def dequeue
raise NotImplementedError, "Not yet implemented"
if @front == @back
raise ArgumentError.new("Queue is empty!")
end

#Store the current first element in a temp
temp = @store[@front]
#Make the front element nil
@store[@front] = nil

@front = (@front + 1) % @store.length

return temp
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Other methods are missing here.

def front
Expand Down
14 changes: 9 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
require_relative './linked_list.rb'

class Stack
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new
end

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

def pop
raise NotImplementedError, "Not yet implemented"
return @store.remove_last if @store.get_first != nil
end

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

def to_s
Expand Down
4 changes: 0 additions & 4 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
end

it "pushes multiple somethings onto a Stack" do
skip
s = Stack.new
s.push(10)
s.push(20)
Expand All @@ -26,13 +25,11 @@
end

it "starts the stack empty" do
skip
s = Stack.new
s.empty?.must_equal true
end

it "removes something from the stack" do
skip
s = Stack.new
s.push(5)
removed = s.pop
Expand All @@ -41,7 +38,6 @@
end

it "removes the right something (LIFO)" do
skip
s = Stack.new
s.push(5)
s.push(3)
Expand Down