Skip to content

Angele Z. #41

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

Open
wants to merge 5 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
66 changes: 59 additions & 7 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,83 @@
class Queue

Choose a reason for hiding this comment

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

Just noting dequeue and size are not implemented


def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
# first in, first out, like a check-out line
@store = Array.new(19)
# pointers to @store nodes
@front = @back = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
# if empty then add element node to index 0
if @front == -1 && @back == -1
puts "setting front and back to 0"
@front = 0
@back = 0
end
# if queue is full
if @front == @back && !@store[@front].nil?
puts "adding #{element}, queue will be full"
puts "(@front: #{@front}, @back: #{@back}"
end
puts "adding #{element} to @back located at index #{@back}"
puts "@front: #{@front}"
@store[@back] = element
@back = (@back + 1) % @store.length
puts "moved @back to #{@back}"
return element
end

def dequeue
raise NotImplementedError, "Not yet implemented"
# if empty return nil
if @front == -1 && @back == -1
return nil
end
# if queue is not empty return the first element in the list (at front)
# and remove it from queue
dequeued = @store[@front]
@store[@front] = nil
@front = (@front + 1) % @store.length
# if it was the last one, set front and back to -1
if @front == @back && @store[@back].nil?
puts "it was the last one, resetting queue"
@front = @back = -1
end
puts "removed #{dequeued} from queue, @front: #{@front}, @back: #{@back}"
return dequeued
end

def front
raise NotImplementedError, "Not yet implemented"
end

def size
raise NotImplementedError, "Not yet implemented"
# from front to end
if @front < @back
size = @store[@front...@back].length
# from back to end, then start to front
else
size = @store[@back..-1] + @store[0...@front]
end
return size
end

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

def to_s

Choose a reason for hiding this comment

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

👍

return @store.to_s
# from front to end
if @front < @back
print_store = @store[@front...@back]
# from front to end, then 0 to back
else
print_store = @store[@front..-1] + @store[0...@back]
end
puts "front and back:"
puts @front
puts @back
puts "actual @store:"
puts @store.to_s
return print_store.to_s
end
end
12 changes: 7 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require 'linked_list'
class Stack
# first in, last out, like a pile of books
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()
end

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

def to_s
Expand Down
35 changes: 26 additions & 9 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
end

it "adds something to an empty Queue" do
skip
q = Queue.new
q.enqueue(10)
q.to_s.must_equal "[10]"
end

it "adds multiple somethings to a Queue" do
skip
q = Queue.new
q.enqueue(10)
q.enqueue(20)
Expand All @@ -27,13 +25,36 @@
end

it "starts the size of a Queue at 0" do
skip
q = Queue.new
q.empty?.must_equal true
end

it "calculates size correctly" do
q = Queue.new
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
expect(q.size).must_equal 3
q.dequeue
expect(q.size).must_equal 2
q.enqueue(40)
q.enqueue(50)
q.enqueue(60)
q.enqueue(70)
q.enqueue(80)
q.enqueue(90)
q.enqueue(100)
q.enqueue(110)
q.enqueue(120)
expect(q.size).must_equal 11
q.dequeue
q.dequeue
q.dequeue
q.dequeue
expect(q.size).must_equal 7
end

it "removes something from the Queue" do
skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -42,7 +63,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -53,7 +73,6 @@
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
q = Queue.new
q.empty?.must_equal true
q.enqueue(-1)
Expand All @@ -65,7 +84,6 @@
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand All @@ -92,7 +110,6 @@
q.enqueue(130)
q.enqueue(140)
q.enqueue(150)
q.enqueue(150)
q.enqueue(160)
q.enqueue(170)
q.enqueue(180)
Expand All @@ -101,6 +118,6 @@
q.enqueue(210)
q.dequeue

expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240]')
expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]')
end
end
5 changes: 0 additions & 5 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
end

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

it "pushes multiple somethings onto a Stack" do
skip
s = Stack.new
s.push(10)
s.push(20)
Expand All @@ -26,13 +24,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 +37,6 @@
end

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