diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..9336dc5c 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,16 +1,37 @@ +# QUEUE_SIZE = 20 class Queue - def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = Array.new(20) + @queue_size = 20 + @front = @rear = -1 end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + if @front == -1 #Q is empty + @front = 0 + @rear = 1 + @store[@front] = element + elsif @front == @rear + raise ArgumentError, "Q FULL" + else #not empty + @store[@rear] = element + @rear = (@rear + 1) % @queue_size + end end def dequeue - raise NotImplementedError, "Not yet implemented" + if @front == -1 + reise ArgumentError, "Queue empty" + else + element = @store[@front] + @front = (@front + 1) % @queue_size + if @front == @rear + @front = -1 + @rear = -1 + end + end + return element end def front @@ -22,10 +43,21 @@ def size end def empty? - raise NotImplementedError, "Not yet implemented" + if @front == -1 + return true + else + return false + end end def to_s - return @store.to_s + list = [] + current = @front + while current != @rear + list << @store[current] + current = (current + 1) % @queue_size + end + return list.to_s end end + diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..0ad4f025 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,22 +1,29 @@ +require_relative "linked_list" + 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) end def pop - raise NotImplementedError, "Not yet implemented" + @store.remove_last() end def empty? - raise NotImplementedError, "Not yet implemented" + last = @store.get_last() + if last == nil + return true + else + return false + end end def to_s return @store.to_s end end + diff --git a/test/queue_test.rb b/test/queue_test.rb index 9b616a30..5cef322b 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -11,14 +11,14 @@ 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) @@ -27,7 +27,7 @@ end it "starts the size of a Queue at 0" do - skip + q = Queue.new q.empty?.must_equal true end @@ -42,7 +42,7 @@ end it "removes the right something (LIFO)" do - skip + q = Queue.new q.enqueue(5) q.enqueue(3) @@ -53,7 +53,7 @@ end it "properly adjusts the size with enqueueing and dequeueing" do - skip + q = Queue.new q.empty?.must_equal true q.enqueue(-1) @@ -65,7 +65,7 @@ end it "returns the front element in the Queue" do - skip + q = Queue.new q.enqueue(40) q.enqueue(22) diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..a790a3d0 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -10,14 +10,14 @@ 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) @@ -26,13 +26,13 @@ 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 @@ -41,7 +41,7 @@ end it "removes the right something (LIFO)" do - skip + s = Stack.new s.push(5) s.push(3)