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 - Jessica #33

Open
wants to merge 6 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

In this exercise we will implement both a stack & a queue, and then use them in a variety of hands-on exercises.

**Due: Monday Sept 7th**
**Due: Tuesday Sept 8th**

## Learning Goals

Expand Down
4 changes: 2 additions & 2 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def remove_first()

value = @head.data
@head = @head.next
@head.previous = nil
@head.previous = nil unless @head.nil?
return value
end

Expand Down Expand Up @@ -339,4 +339,4 @@ def to_s

return list.to_s
end
end
end
48 changes: 40 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
class Queue

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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if (@front == 0 && @rear == @size - 1) || (@rear == (@front - 1) % (@size-1))
raise ArgumentError.new('Queue is full')
elsif @front == -1 && @rear == -1
@front = @rear = 0
elsif @rear == @size - 1 && @front != 0
@rear = 0
else
@rear = @rear + 1
end

@store[@rear] = element
end

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

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

if @front == @rear
@front = @rear = -1
elsif @front == @size - 1
@front = 0
else
@front = @front + 1
end

return data
end

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

def size
raise NotImplementedError, "Not yet implemented"
return @back >= @front ? (@back - @front) : (@size - @front + @back)
end

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

def to_s
return @store.to_s
queue = []
@size.times do |i|
index = (i + @front) % @size
queue << @store[index] if @store[index]
end
return queue.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
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?

return @store.remove_first()
end

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

def to_s
Expand Down
17 changes: 9 additions & 8 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
describe "Test Stack Implementation" do
it "creates a Stack" do
s = Stack.new
s.class.must_equal Stack
expect(s.class).must_equal Stack
end

it "pushes something onto a empty Stack" do
s = Stack.new
s.push(10)
s.to_s.must_equal "[10]"
expect(s.pop).must_equal 10
end

it "pushes multiple somethings onto a Stack" do
s = Stack.new
s.push(10)
s.push(20)
s.push(30)
s.to_s.must_equal "[10, 20, 30]"
expect(s.pop).must_equal 30
expect(s.pop).must_equal 20
expect(s.pop).must_equal 10
end

it "starts the stack empty" do
Expand All @@ -29,8 +31,8 @@
s = Stack.new
s.push(5)
removed = s.pop
removed.must_equal 5
s.empty?.must_equal true
expect(removed).must_equal 5
expect(s.empty?).must_equal true
end

it "removes the right something (LIFO)" do
Expand All @@ -39,7 +41,6 @@
s.push(3)
s.push(7)
removed = s.pop
removed.must_equal 7
s.to_s.must_equal "[5, 3]"
expect(removed).must_equal 7
end
end
end