From 42b8f8550d7a20477533b7490807ad1bfb27ffdd Mon Sep 17 00:00:00 2001 From: Chu Date: Thu, 12 Sep 2019 11:59:23 -0700 Subject: [PATCH 1/6] stack tests passed --- lib/linked_list.rb | 6 +- lib/stack.rb | 11 ++-- test/problems_test.rb | 114 +++++++++++++++++------------------ test/queue_test.rb | 134 +++++++++++++++++++++--------------------- test/stack_test.rb | 18 +++--- 5 files changed, 144 insertions(+), 139 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 136d8ac9..b535104a 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -169,12 +169,13 @@ def reverse prev = nil current = @head until current.nil? + # placeholder for node #2 temp = current.next + # next node is node #-1 current.next = prev prev = current current = temp prev.previous = current - end @head = prev @@ -301,6 +302,7 @@ def insert_ascending(value) end until current.nil? + # until current.next == nil, will run into nil > value error otherwise, or until current == @tail if current.data <= value && current.next.data > value temp = current.next current.next = new_node @@ -311,6 +313,8 @@ def insert_ascending(value) end current = current.next end + + # add node as tail end # Helper method for tests diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..70c7d1aa 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,20 @@ +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) end def pop - raise NotImplementedError, "Not yet implemented" + @store.remove_last end def empty? - raise NotImplementedError, "Not yet implemented" + return @store.empty? end def to_s diff --git a/test/problems_test.rb b/test/problems_test.rb index f851f1d2..95b985f5 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -1,57 +1,57 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/problems' - -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new - -describe "Test wave 3 problems" do - describe "balanced" do - it "Given balanced strings it should return true" do - skip - expect(balanced('(({}))')).must_equal true - end - - it "regards an empty string as balanced" do - skip - expect(balanced('')).must_equal true - end - - it "will return false for an unbalanced set of parens" do - skip - expect(balanced('(()')).must_equal false - expect(balanced('(()}')).must_equal false - expect(balanced('([]]')).must_equal false - end - - it "also works for {} and []" do - skip - expect(balanced('[]')).must_equal true - expect(balanced('{}')).must_equal true - end - - it "also works if the string has opens and closes in the beginning and end" do - skip - expect(balanced('[]()')).must_equal true - end - end - - describe "postfix" do - it "can add a 2 numbers together" do - skip - expect(evaluate_postfix("34+")).must_equal 7 - expect(evaluate_postfix("34*")).must_equal 12 - expect(evaluate_postfix("34-")).must_equal -1 - expect(evaluate_postfix("34/")).must_equal 0 - end - - it "can add a evaluate a more complicated expression" do - skip - expect(evaluate_postfix("34+2*")).must_equal 14 - expect(evaluate_postfix("34*2/")).must_equal 6 - expect(evaluate_postfix("34-1+")).must_equal 0 - expect(evaluate_postfix("34/7-")).must_equal -7 - expect(evaluate_postfix("35+6*")).must_equal 48 - expect(evaluate_postfix("62/5+")).must_equal 8 - end - end -end \ No newline at end of file +# require 'minitest/autorun' +# require 'minitest/reporters' +# require_relative '../lib/problems' + +# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +# describe "Test wave 3 problems" do +# describe "balanced" do +# it "Given balanced strings it should return true" do +# skip +# expect(balanced('(({}))')).must_equal true +# end + +# it "regards an empty string as balanced" do +# skip +# expect(balanced('')).must_equal true +# end + +# it "will return false for an unbalanced set of parens" do +# skip +# expect(balanced('(()')).must_equal false +# expect(balanced('(()}')).must_equal false +# expect(balanced('([]]')).must_equal false +# end + +# it "also works for {} and []" do +# skip +# expect(balanced('[]')).must_equal true +# expect(balanced('{}')).must_equal true +# end + +# it "also works if the string has opens and closes in the beginning and end" do +# skip +# expect(balanced('[]()')).must_equal true +# end +# end + +# describe "postfix" do +# it "can add a 2 numbers together" do +# skip +# expect(evaluate_postfix("34+")).must_equal 7 +# expect(evaluate_postfix("34*")).must_equal 12 +# # expect(evaluate_postfix("34(-) ")).must_equal -1 +# expect(evaluate_postfix("34/")).must_equal 0 +# end + +# it "can add a evaluate a more complicated expression" do +# skip +# expect(evaluate_postfix("34+2*")).must_equal 14 +# expect(evaluate_postfix("34*2/")).must_equal 6 +# expect(evaluate_postfix("34-1+")).must_equal 0 +# # expect(evaluate_postfix("34/7(-)")).must_equal -7 +# expect(evaluate_postfix("35+6*")).must_equal 48 +# expect(evaluate_postfix("62/5+")).must_equal 8 +# end +# end +# end diff --git a/test/queue_test.rb b/test/queue_test.rb index 9b616a30..b24939d0 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -1,76 +1,76 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/queue' +# require 'minitest/autorun' +# require 'minitest/reporters' +# require_relative '../lib/queue' -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe "Test Queue Implementation" do - it "creates a Queue" do - q = Queue.new - q.class.must_equal Queue - end +# describe "Test Queue Implementation" do +# it "creates a Queue" do +# q = Queue.new +# q.class.must_equal Queue +# 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 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) - q.enqueue(30) - q.to_s.must_equal "[10, 20, 30]" - end +# it "adds multiple somethings to a Queue" do +# skip +# q = Queue.new +# q.enqueue(10) +# q.enqueue(20) +# q.enqueue(30) +# q.to_s.must_equal "[10, 20, 30]" +# end - it "starts the size of a Queue at 0" do - skip - q = Queue.new - q.empty?.must_equal true - end +# it "starts the size of a Queue at 0" do +# skip +# q = Queue.new +# q.empty?.must_equal true +# end - it "removes something from the Queue" do - skip - q = Queue.new - q.enqueue(5) - removed = q.dequeue - removed.must_equal 5 - q.empty?.must_equal true - end +# it "removes something from the Queue" do +# skip +# q = Queue.new +# q.enqueue(5) +# removed = q.dequeue +# removed.must_equal 5 +# q.empty?.must_equal true +# end - it "removes the right something (LIFO)" do - skip - q = Queue.new - q.enqueue(5) - q.enqueue(3) - q.enqueue(7) - removed = q.dequeue - removed.must_equal 5 - q.to_s.must_equal "[3, 7]" - end +# it "removes the right something (LIFO)" do +# skip +# q = Queue.new +# q.enqueue(5) +# q.enqueue(3) +# q.enqueue(7) +# removed = q.dequeue +# removed.must_equal 5 +# q.to_s.must_equal "[3, 7]" +# end - it "properly adjusts the size with enqueueing and dequeueing" do - skip - q = Queue.new - q.empty?.must_equal true - q.enqueue(-1) - q.enqueue(-60) - q.empty?.must_equal false - q.dequeue - q.dequeue - q.empty?.must_equal true - end +# it "properly adjusts the size with enqueueing and dequeueing" do +# skip +# q = Queue.new +# q.empty?.must_equal true +# q.enqueue(-1) +# q.enqueue(-60) +# q.empty?.must_equal false +# q.dequeue +# q.dequeue +# q.empty?.must_equal true +# end - it "returns the front element in the Queue" do - skip - q = Queue.new - q.enqueue(40) - q.enqueue(22) - q.enqueue(3) - q.dequeue - expect(q.dequeue).must_equal 22 - end -end \ No newline at end of file +# it "returns the front element in the Queue" do +# skip +# q = Queue.new +# q.enqueue(40) +# q.enqueue(22) +# q.enqueue(3) +# q.dequeue +# expect(q.dequeue).must_equal 22 +# end +# end diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..a29943f3 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -1,6 +1,6 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/stack' +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/stack" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe "Test Stack Implementation" do @@ -10,14 +10,14 @@ end it "pushes something onto a empty Stack" do - skip + # skip s = Stack.new s.push(10) s.to_s.must_equal "[10]" end it "pushes multiple somethings onto a Stack" do - skip + # skip s = Stack.new s.push(10) s.push(20) @@ -26,13 +26,13 @@ end it "starts the stack empty" do - skip + # skip s = Stack.new s.empty?.must_equal true end it "removes something from the stack" do - skip + # skip s = Stack.new s.push(5) removed = s.pop @@ -41,7 +41,7 @@ end it "removes the right something (LIFO)" do - skip + # skip s = Stack.new s.push(5) s.push(3) @@ -50,4 +50,4 @@ removed.must_equal 7 s.to_s.must_equal "[5, 3]" end -end \ No newline at end of file +end From f88b5e8c3589aa42c6a9bcc4c026c06625754a29 Mon Sep 17 00:00:00 2001 From: Chu Date: Thu, 12 Sep 2019 12:09:37 -0700 Subject: [PATCH 2/6] passed q tests --- lib/linked_list.rb | 546 ++++++++++++++++++++++----------------------- lib/queue.rb | 16 +- test/queue_test.rb | 135 +++++------ test/stack_test.rb | 94 ++++---- 4 files changed, 393 insertions(+), 398 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b535104a..19b5f450 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,335 +12,329 @@ def initialize(value, next_node = nil, previous_node = nil) # Defines the singly linked list class LinkedList - def initialize - @head = nil # keep the head private. Not accessible outside this class - @tail = nil - end - - # method to add a new node with the specific data value in the linked list - # insert the new node at the beginning of the linked list - # Time Complexity: O(1) - # Space Complexity O(1) - def add_first(value) - new_node = Node.new(value) - new_node.next = @head + def initialize + @head = nil # keep the head private. Not accessible outside this class + @tail = nil + end - @head.previous = new_node unless @head.nil? - @head = new_node - if @tail.nil? - @tail = @head - end + # method to add a new node with the specific data value in the linked list + # insert the new node at the beginning of the linked list + # Time Complexity: O(1) + # Space Complexity O(1) + def add_first(value) + new_node = Node.new(value) + new_node.next = @head + + @head.previous = new_node unless @head.nil? + @head = new_node + if @tail.nil? + @tail = @head end + end - def remove_first() - raise ArgumentError, "Empty" if self.empty? + def remove_first() + raise ArgumentError, "Empty" if self.empty? - value = @head.data - @head = @head.next - @head.previous = nil - return value - end + value = @head.data + @head = @head.next + @head.previous = nil if @head + return value + end - def empty? - return @head.nil? - end + def empty? + return @head.nil? + end - # method to find if the linked list contains a node with specified value - # returns true if found, false otherwise - # Time Complexity: O(n) - # Space Complexity: O(1) - def search(value) - return false if @head.nil? - return true if @head.data = value - current = @head - until current.data.nil? - return true if current.data = value - current = current.next - end + # method to find if the linked list contains a node with specified value + # returns true if found, false otherwise + # Time Complexity: O(n) + # Space Complexity: O(1) + def search(value) + return false if @head.nil? + return true if @head.data = value + current = @head + until current.data.nil? + return true if current.data = value + current = current.next end + end - # method to return the max value in the linked list - # returns the data value and not the node - # Time Complexity: O(n) - # Space Complexity: O(1) - def find_max - return nil if @head.nil? - current = @head - max = current.data - until current.nil? - max = current.data if current.data > max - current = current.next - end - return max + # method to return the max value in the linked list + # returns the data value and not the node + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_max + return nil if @head.nil? + current = @head + max = current.data + until current.nil? + max = current.data if current.data > max + current = current.next end + return max + end - # method to return the min value in the linked list - # returns the data value and not the node - # Time Complexity: O(n) - # Space Complexity: O(1) - def find_min - return nil if @head.nil? - current = @head - min = current.data - until current.nil? - if current.data < min - min = current.data - end - current = current.next + # method to return the min value in the linked list + # returns the data value and not the node + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_min + return nil if @head.nil? + current = @head + min = current.data + until current.nil? + if current.data < min + min = current.data end - return min + current = current.next end + return min + end - - # method that returns the length of the singly linked list - # Time Complexity: O(n) - # Space Complexity: O(1) - def length - return 0 if @head.nil? - length = 0 - current = @head - until current.nil? - current = current.next - length += 1 - end - return length - + # method that returns the length of the singly linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def length + return 0 if @head.nil? + length = 0 + current = @head + until current.nil? + current = current.next + length += 1 end + return length + end - # method that returns the value at a given index in the linked list - # index count starts at 0 - # returns nil if there are fewer nodes in the linked list than the index value - # Time Complexity: O(n) - # Space Complexity: O(1) - def get_at_index(index) - return nil if length <= index - current = @head - count = 0 - until current.nil? - return current.data if count == index - current = current.next - count += 1 - end + # method that returns the value at a given index in the linked list + # index count starts at 0 + # returns nil if there are fewer nodes in the linked list than the index value + # Time Complexity: O(n) + # Space Complexity: O(1) + def get_at_index(index) + return nil if length <= index + current = @head + count = 0 + until current.nil? + return current.data if count == index + current = current.next + count += 1 end + end - # method to print all the values in the linked list - # Time Complexity: O(n) - # Space Complexity: O(1) - def visit - current = @head - until current.nil? - puts current.data - current = current.next - end + # method to print all the values in the linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def visit + current = @head + until current.nil? + puts current.data + current = current.next end + end - # method to delete the first node found with specified value - # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(1) - def delete(value) - return if @head.nil? - current = @head + # method to delete the first node found with specified value + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) + def delete(value) + return if @head.nil? + current = @head + + if current.data == value + @head = current.next + @head.previous = nil unless @head.nil? + return + end + prev = current + until current.nil? if current.data == value - @head = current.next - @head.previous = nil unless @head.nil? - return + prev.next = current.next + current.next.previous = prev unless current.next.nil? + @tail = prev if @tail == current + else + prev = current end + current = current.next + end + end + # method to reverse the singly linked list + # note: the nodes should be moved and not just the values in the nodes + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) + def reverse + return nil if @head.nil? + prev = nil + current = @head + until current.nil? + # placeholder for node #2 + temp = current.next + # next node is node #-1 + current.next = prev prev = current - until current.nil? - if current.data == value - prev.next = current.next - current.next.previous = prev unless current.next.nil? - @tail = prev if @tail == current - else - prev = current - end - current = current.next - end + current = temp + prev.previous = current end - # method to reverse the singly linked list - # note: the nodes should be moved and not just the values in the nodes - # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(1) - def reverse - return nil if @head.nil? - prev = nil - current = @head - until current.nil? - # placeholder for node #2 - temp = current.next - # next node is node #-1 - current.next = prev - prev = current - current = temp - prev.previous = current - end - - @head = prev + @head = prev + end + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_middle_value + return nil if @head.nil? + current = @head + if length % 2 == 0 + return nil + else + middle_index = length / 2 end - - - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - # Time Complexity: O(n) - # Space Complexity: O(1) - def find_middle_value - return nil if @head.nil? - current = @head - if length % 2 == 0 - return nil - else - middle_index = length / 2 - end - index = 0 - until current.nil? - return current.data if index == middle_index - current = current.next - index += 1 - end - + index = 0 + until current.nil? + return current.data if index == middle_index + current = current.next + index += 1 end + end - # find the nth node from the end and return its value - # assume indexing starts at 0 while counting to n - # Time Complexity: O(n) - # Space Complexity: O(1) - def find_nth_from_end(n) - return nil if @head.nil? - current = @head - index = 0 - until current.nil? - if index == length - n - 1 - return current.data - end - index += 1 - current = current.next + # find the nth node from the end and return its value + # assume indexing starts at 0 while counting to n + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_nth_from_end(n) + return nil if @head.nil? + current = @head + index = 0 + until current.nil? + if index == length - n - 1 + return current.data end + index += 1 + current = current.next end + end - # checks if the linked list has a cycle. A cycle exists if any node in the - # linked list links to a node already visited. - # returns true if a cycle is found, false otherwise. - # Time Complexity: O(n) - # Space Complexity: O(1) - def has_cycle - return nil if @head.nil? - slow_p = @head - fast_p = @head - while slow_p != nil && fast_p != nil and fast_p.next != nil - slow_p = slow_p.next - fast_p = fast_p.next.next - if slow_p == fast_p - return true - end - return false + # checks if the linked list has a cycle. A cycle exists if any node in the + # linked list links to a node already visited. + # returns true if a cycle is found, false otherwise. + # Time Complexity: O(n) + # Space Complexity: O(1) + def has_cycle + return nil if @head.nil? + slow_p = @head + fast_p = @head + while slow_p != nil && fast_p != nil and fast_p.next != nil + slow_p = slow_p.next + fast_p = fast_p.next.next + if slow_p == fast_p + return true end + return false end + end + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: O(1) + # Space Complexity: O(1) + def get_first + return nil if @head.nil? + return @head.data + end - # Additional Exercises - # returns the value in the first node - # returns nil if the list is empty - # Time Complexity: O(1) - # Space Complexity: O(1) - def get_first - return nil if @head.nil? - return @head.data + # method that inserts a given value as a new last node in the linked list + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) + def add_last(value) + new_node = Node.new(value) + if @head.nil? + self.add_first(value) + return end + @tail.next = new_node + new_node.previous = @tail + @tail = new_node + end - # method that inserts a given value as a new last node in the linked list - # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(1) - def add_last(value) - new_node = Node.new(value) - if @head.nil? - self.add_first(value) - return - end - @tail.next = new_node - new_node.previous = @tail - @tail = new_node + def remove_last() + value = @tail.data + if @head == @tail + @head = @tail = nil + else + @tail = @tail.previous + @tail.next = nil end - def remove_last() - value = @tail.data - if @head == @tail - @head = @tail = nil - else - @tail = @tail.previous - @tail.next = nil - end + return value + end - return value - end + # method that returns the value of the last node in the linked list + # returns nil if the linked list is empty + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) + def get_last + return nil if @head.nil? + return @tail.data + end - # method that returns the value of the last node in the linked list - # returns nil if the linked list is empty - # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(1) - def get_last - return nil if @head.nil? - return @tail.data + # method to insert a new node with specific data value, assuming the linked + # list is sorted in ascending order + # Time Complexity: O(n) + # Space Complexity: O(1) + def insert_ascending(value) + new_node = Node.new(value) + add_first(value) if @head.nil? + current = @head + if current.data > new_node.data + temp = @head + @head = new_node + temp.previous = @head + new_node.next = temp end - # method to insert a new node with specific data value, assuming the linked - # list is sorted in ascending order - # Time Complexity: O(n) - # Space Complexity: O(1) - def insert_ascending(value) - new_node = Node.new(value) - add_first(value) if @head.nil? - current = @head - if current.data > new_node.data - temp = @head - @head = new_node - temp.previous = @head + until current.nil? + # until current.next == nil, will run into nil > value error otherwise, or until current == @tail + if current.data <= value && current.next.data > value + temp = current.next + current.next = new_node new_node.next = temp + new_node.previous = current + temp.previous = new_node + return end - - until current.nil? - # until current.next == nil, will run into nil > value error otherwise, or until current == @tail - if current.data <= value && current.next.data > value - temp = current.next - current.next = new_node - new_node.next = temp - new_node.previous = current - temp.previous = new_node - return - end - current = current.next - end - - # add node as tail + current = current.next end - # Helper method for tests - # Creates a cycle in the linked list for testing purposes - # Assumes the linked list has at least one node - def create_cycle - return if @head == nil # don't do anything if the linked list is empty + # add node as tail + end - # navigate to last node - current = @head - while current.next != nil - current = current.next - end + # Helper method for tests + # Creates a cycle in the linked list for testing purposes + # Assumes the linked list has at least one node + def create_cycle + return if @head == nil # don't do anything if the linked list is empty - current.next = @head # make the last node link to first node + # navigate to last node + current = @head + while current.next != nil + current = current.next end - def to_s - list = [] + current.next = @head # make the last node link to first node + end - current = @head - until current.nil? - list << current.data - current = current.next - end + def to_s + list = [] - return list.to_s + current = @head + until current.nil? + list << current.data + current = current.next end -end \ No newline at end of file + + return list.to_s + end +end diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..61b35b53 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,28 +1,28 @@ -class Queue +require_relative "./linked_list.rb" +class Queue def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + @store.add_last(element) end def dequeue - raise NotImplementedError, "Not yet implemented" + @store.remove_first end def front - raise NotImplementedError, "Not yet implemented" + return @store.head end def size - raise NotImplementedError, "Not yet implemented" + return @store.length end def empty? - raise NotImplementedError, "Not yet implemented" + return @store.empty? end def to_s diff --git a/test/queue_test.rb b/test/queue_test.rb index b24939d0..ba3aa901 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -1,76 +1,77 @@ -# require 'minitest/autorun' -# require 'minitest/reporters' -# require_relative '../lib/queue' +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/queue" -# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# describe "Test Queue Implementation" do -# it "creates a Queue" do -# q = Queue.new -# q.class.must_equal Queue -# end +describe "Test Queue Implementation" do + it "creates a Queue" do + q = Queue.new + q.class.must_equal Queue + 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 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) -# q.enqueue(30) -# q.to_s.must_equal "[10, 20, 30]" -# end + it "adds multiple somethings to a Queue" do + # skip + q = Queue.new + q.enqueue(10) + q.enqueue(20) + q.enqueue(30) + q.to_s.must_equal "[10, 20, 30]" + end -# it "starts the size of a Queue at 0" do -# skip -# q = Queue.new -# q.empty?.must_equal true -# end + it "starts the size of a Queue at 0" do + # skip + q = Queue.new + q.empty?.must_equal true + end -# it "removes something from the Queue" do -# skip -# q = Queue.new -# q.enqueue(5) -# removed = q.dequeue -# removed.must_equal 5 -# q.empty?.must_equal true -# end + it "removes something from the Queue" do + # skip + q = Queue.new + q.enqueue(5) + removed = q.dequeue + removed.must_equal 5 + q.empty?.must_equal true + end -# it "removes the right something (LIFO)" do -# skip -# q = Queue.new -# q.enqueue(5) -# q.enqueue(3) -# q.enqueue(7) -# removed = q.dequeue -# removed.must_equal 5 -# q.to_s.must_equal "[3, 7]" -# end + it "removes the right something (LIFO)" do + # skip + q = Queue.new + q.enqueue(5) + q.enqueue(3) + q.enqueue(7) + removed = q.dequeue + removed.must_equal 5 + q.to_s.must_equal "[3, 7]" + end -# it "properly adjusts the size with enqueueing and dequeueing" do -# skip -# q = Queue.new -# q.empty?.must_equal true -# q.enqueue(-1) -# q.enqueue(-60) -# q.empty?.must_equal false -# q.dequeue -# q.dequeue -# q.empty?.must_equal true -# end + it "properly adjusts the size with enqueueing and dequeueing" do + # skip + q = Queue.new + q.empty?.must_equal true + q.enqueue(-1) + q.enqueue(-60) + q.empty?.must_equal false + q.size.must_equal 2 + q.dequeue + q.dequeue + q.empty?.must_equal true + end -# it "returns the front element in the Queue" do -# skip -# q = Queue.new -# q.enqueue(40) -# q.enqueue(22) -# q.enqueue(3) -# q.dequeue -# expect(q.dequeue).must_equal 22 -# end -# end + it "returns the front element in the Queue" do + # skip + q = Queue.new + q.enqueue(40) + q.enqueue(22) + q.enqueue(3) + q.dequeue + expect(q.dequeue).must_equal 22 + end +end diff --git a/test/stack_test.rb b/test/stack_test.rb index a29943f3..c58be71d 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -1,53 +1,53 @@ -require "minitest/autorun" -require "minitest/reporters" -require_relative "../lib/stack" -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +# require "minitest/autorun" +# require "minitest/reporters" +# require_relative "../lib/stack" +# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe "Test Stack Implementation" do - it "creates a Stack" do - s = Stack.new - s.class.must_equal Stack - end +# describe "Test Stack Implementation" do +# it "creates a Stack" do +# s = Stack.new +# s.class.must_equal Stack +# 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 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) - s.push(30) - s.to_s.must_equal "[10, 20, 30]" - end +# it "pushes multiple somethings onto a Stack" do +# # skip +# s = Stack.new +# s.push(10) +# s.push(20) +# s.push(30) +# s.to_s.must_equal "[10, 20, 30]" +# end - it "starts the stack empty" do - # skip - s = Stack.new - s.empty?.must_equal true - 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 - removed.must_equal 5 - s.empty?.must_equal true - end +# it "removes something from the stack" do +# # skip +# s = Stack.new +# s.push(5) +# removed = s.pop +# removed.must_equal 5 +# s.empty?.must_equal true +# end - it "removes the right something (LIFO)" do - # skip - s = Stack.new - s.push(5) - s.push(3) - s.push(7) - removed = s.pop - removed.must_equal 7 - s.to_s.must_equal "[5, 3]" - end -end +# it "removes the right something (LIFO)" do +# # skip +# s = Stack.new +# s.push(5) +# s.push(3) +# s.push(7) +# removed = s.pop +# removed.must_equal 7 +# s.to_s.must_equal "[5, 3]" +# end +# end From b266f7f075f65867938e293d079bb73000215184 Mon Sep 17 00:00:00 2001 From: Chu Date: Thu, 12 Sep 2019 12:54:02 -0700 Subject: [PATCH 3/6] string balance works! --- lib/problems.rb | 22 ++++++- test/problems_test.rb | 114 +++++++++++++++++------------------ test/queue_test.rb | 136 +++++++++++++++++++++--------------------- 3 files changed, 144 insertions(+), 128 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 61bcaa5a..d545dec3 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,9 +1,25 @@ -require_relative './stack.rb' +require_relative "./stack.rb" def balanced(string) - raise NotImplementedError, "Not implemented yet" + return true if string == "" + stack = Stack.new + char_map = { + "}" => "{", + "]" => "[", + ")" => "(", + } + open_chars = ["{", "[", "("] + string.each_char do |c| + if open_chars.include?(c) + stack.push(c) + else + pop_c = stack.pop + return false unless pop_c && pop_c == char_map[c] + end + end + return stack.empty? end def evaluate_postfix(postfix_expression) raise NotImplementedError, "Not implemented yet" -end \ No newline at end of file +end diff --git a/test/problems_test.rb b/test/problems_test.rb index 95b985f5..982060e1 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -1,57 +1,57 @@ -# require 'minitest/autorun' -# require 'minitest/reporters' -# require_relative '../lib/problems' - -# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new - -# describe "Test wave 3 problems" do -# describe "balanced" do -# it "Given balanced strings it should return true" do -# skip -# expect(balanced('(({}))')).must_equal true -# end - -# it "regards an empty string as balanced" do -# skip -# expect(balanced('')).must_equal true -# end - -# it "will return false for an unbalanced set of parens" do -# skip -# expect(balanced('(()')).must_equal false -# expect(balanced('(()}')).must_equal false -# expect(balanced('([]]')).must_equal false -# end - -# it "also works for {} and []" do -# skip -# expect(balanced('[]')).must_equal true -# expect(balanced('{}')).must_equal true -# end - -# it "also works if the string has opens and closes in the beginning and end" do -# skip -# expect(balanced('[]()')).must_equal true -# end -# end - -# describe "postfix" do -# it "can add a 2 numbers together" do -# skip -# expect(evaluate_postfix("34+")).must_equal 7 -# expect(evaluate_postfix("34*")).must_equal 12 -# # expect(evaluate_postfix("34(-) ")).must_equal -1 -# expect(evaluate_postfix("34/")).must_equal 0 -# end - -# it "can add a evaluate a more complicated expression" do -# skip -# expect(evaluate_postfix("34+2*")).must_equal 14 -# expect(evaluate_postfix("34*2/")).must_equal 6 -# expect(evaluate_postfix("34-1+")).must_equal 0 -# # expect(evaluate_postfix("34/7(-)")).must_equal -7 -# expect(evaluate_postfix("35+6*")).must_equal 48 -# expect(evaluate_postfix("62/5+")).must_equal 8 -# end -# end -# end +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/problems" + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe "Test wave 3 problems" do + describe "balanced" do + it "Given balanced strings it should return true" do + # skip + expect(balanced("(({}))")).must_equal true + end + + it "regards an empty string as balanced" do + # skip + expect(balanced("")).must_equal true + end + + it "will return false for an unbalanced set of parens" do + # skip + expect(balanced("(()")).must_equal false + expect(balanced("(()}")).must_equal false + expect(balanced("([]]")).must_equal false + end + + it "also works for {} and []" do + # skip + expect(balanced("[]")).must_equal true + expect(balanced("{}")).must_equal true + end + + it "also works if the string has opens and closes in the beginning and end" do + # skip + expect(balanced("[]()")).must_equal true + end + end + + describe "postfix" do + it "can add a 2 numbers together" do + # skip + expect(evaluate_postfix("34+")).must_equal 7 + expect(evaluate_postfix("34*")).must_equal 12 + # expect(evaluate_postfix("34(-) ")).must_equal -1 + expect(evaluate_postfix("34/")).must_equal 0 + end + + it "can add a evaluate a more complicated expression" do + # skip + expect(evaluate_postfix("34+2*")).must_equal 14 + expect(evaluate_postfix("34*2/")).must_equal 6 + expect(evaluate_postfix("34-1+")).must_equal 0 + # expect(evaluate_postfix("34/7(-)")).must_equal -7 + expect(evaluate_postfix("35+6*")).must_equal 48 + expect(evaluate_postfix("62/5+")).must_equal 8 + end + end +end diff --git a/test/queue_test.rb b/test/queue_test.rb index ba3aa901..bdbacffd 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -1,77 +1,77 @@ -require "minitest/autorun" -require "minitest/reporters" -require_relative "../lib/queue" +# require "minitest/autorun" +# require "minitest/reporters" +# require_relative "../lib/queue" -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe "Test Queue Implementation" do - it "creates a Queue" do - q = Queue.new - q.class.must_equal Queue - end +# describe "Test Queue Implementation" do +# it "creates a Queue" do +# q = Queue.new +# q.class.must_equal Queue +# 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 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) - q.enqueue(30) - q.to_s.must_equal "[10, 20, 30]" - end +# it "adds multiple somethings to a Queue" do +# # skip +# q = Queue.new +# q.enqueue(10) +# q.enqueue(20) +# q.enqueue(30) +# q.to_s.must_equal "[10, 20, 30]" +# end - it "starts the size of a Queue at 0" do - # skip - q = Queue.new - q.empty?.must_equal true - end +# it "starts the size of a Queue at 0" do +# # skip +# q = Queue.new +# q.empty?.must_equal true +# end - it "removes something from the Queue" do - # skip - q = Queue.new - q.enqueue(5) - removed = q.dequeue - removed.must_equal 5 - q.empty?.must_equal true - end +# it "removes something from the Queue" do +# # skip +# q = Queue.new +# q.enqueue(5) +# removed = q.dequeue +# removed.must_equal 5 +# q.empty?.must_equal true +# end - it "removes the right something (LIFO)" do - # skip - q = Queue.new - q.enqueue(5) - q.enqueue(3) - q.enqueue(7) - removed = q.dequeue - removed.must_equal 5 - q.to_s.must_equal "[3, 7]" - end +# it "removes the right something (LIFO)" do +# # skip +# q = Queue.new +# q.enqueue(5) +# q.enqueue(3) +# q.enqueue(7) +# removed = q.dequeue +# removed.must_equal 5 +# q.to_s.must_equal "[3, 7]" +# end - it "properly adjusts the size with enqueueing and dequeueing" do - # skip - q = Queue.new - q.empty?.must_equal true - q.enqueue(-1) - q.enqueue(-60) - q.empty?.must_equal false - q.size.must_equal 2 - q.dequeue - q.dequeue - q.empty?.must_equal true - end +# it "properly adjusts the size with enqueueing and dequeueing" do +# # skip +# q = Queue.new +# q.empty?.must_equal true +# q.enqueue(-1) +# q.enqueue(-60) +# q.empty?.must_equal false +# q.size.must_equal 2 +# q.dequeue +# q.dequeue +# q.empty?.must_equal true +# end - it "returns the front element in the Queue" do - # skip - q = Queue.new - q.enqueue(40) - q.enqueue(22) - q.enqueue(3) - q.dequeue - expect(q.dequeue).must_equal 22 - end -end +# it "returns the front element in the Queue" do +# # skip +# q = Queue.new +# q.enqueue(40) +# q.enqueue(22) +# q.enqueue(3) +# q.dequeue +# expect(q.dequeue).must_equal 22 +# end +# end From 692c437884cec2b6ce31fc9d9f5498aa90778b5f Mon Sep 17 00:00:00 2001 From: Chu Date: Thu, 12 Sep 2019 12:56:26 -0700 Subject: [PATCH 4/6] added notes to balanced --- lib/problems.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/problems.rb b/lib/problems.rb index d545dec3..718ad083 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -2,6 +2,8 @@ def balanced(string) return true if string == "" + + # set up stack = Stack.new char_map = { "}" => "{", @@ -9,6 +11,12 @@ def balanced(string) ")" => "(", } open_chars = ["{", "[", "("] + + # loop through each char + # push to stack if it's opening char. if not, look up corresponding value in hash + # compare hash value to popped stack node. if they are not equal or hash value is nil, return false + # finally, check if the stack is empty. if it is, it means everything matches up + string.each_char do |c| if open_chars.include?(c) stack.push(c) From 6938bdc41848af22c584c5eaa9aa5acc0a923634 Mon Sep 17 00:00:00 2001 From: Chu Date: Fri, 13 Sep 2019 11:08:29 -0700 Subject: [PATCH 5/6] all tests passed (finished postfix) --- lib/problems.rb | 27 ++++++++- test/problems_test.rb | 4 +- test/queue_test.rb | 136 +++++++++++++++++++++--------------------- test/stack_test.rb | 94 ++++++++++++++--------------- 4 files changed, 143 insertions(+), 118 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 718ad083..06999bc7 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -29,5 +29,30 @@ def balanced(string) end def evaluate_postfix(postfix_expression) - raise NotImplementedError, "Not implemented yet" + num_stack = Stack.new + postfix_expression.each_char do |c| + case c + # when it's an operand, pop both operands out, perform operation, push back to stack + when "+" + num2 = num_stack.pop + num1 = num_stack.pop + num_stack.push(num1 + num2) + when "-" + num2 = num_stack.pop + num1 = num_stack.pop + num_stack.push(num1 - num2) + when "*" + num2 = num_stack.pop + num1 = num_stack.pop + num_stack.push(num1 * num2) + when "/" + num2 = num_stack.pop + num1 = num_stack.pop + num_stack.push(num1 / num2) + else + # push to stack + num_stack.push(c.to_i) + end + end + return num_stack.pop end diff --git a/test/problems_test.rb b/test/problems_test.rb index 982060e1..39b28d6e 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -40,7 +40,7 @@ # skip expect(evaluate_postfix("34+")).must_equal 7 expect(evaluate_postfix("34*")).must_equal 12 - # expect(evaluate_postfix("34(-) ")).must_equal -1 + expect(evaluate_postfix("34-")).must_equal -1 expect(evaluate_postfix("34/")).must_equal 0 end @@ -49,7 +49,7 @@ expect(evaluate_postfix("34+2*")).must_equal 14 expect(evaluate_postfix("34*2/")).must_equal 6 expect(evaluate_postfix("34-1+")).must_equal 0 - # expect(evaluate_postfix("34/7(-)")).must_equal -7 + expect(evaluate_postfix("34/7-")).must_equal -7 expect(evaluate_postfix("35+6*")).must_equal 48 expect(evaluate_postfix("62/5+")).must_equal 8 end diff --git a/test/queue_test.rb b/test/queue_test.rb index bdbacffd..ba3aa901 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -1,77 +1,77 @@ -# require "minitest/autorun" -# require "minitest/reporters" -# require_relative "../lib/queue" +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/queue" -# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# describe "Test Queue Implementation" do -# it "creates a Queue" do -# q = Queue.new -# q.class.must_equal Queue -# end +describe "Test Queue Implementation" do + it "creates a Queue" do + q = Queue.new + q.class.must_equal Queue + 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 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) -# q.enqueue(30) -# q.to_s.must_equal "[10, 20, 30]" -# end + it "adds multiple somethings to a Queue" do + # skip + q = Queue.new + q.enqueue(10) + q.enqueue(20) + q.enqueue(30) + q.to_s.must_equal "[10, 20, 30]" + end -# it "starts the size of a Queue at 0" do -# # skip -# q = Queue.new -# q.empty?.must_equal true -# end + it "starts the size of a Queue at 0" do + # skip + q = Queue.new + q.empty?.must_equal true + end -# it "removes something from the Queue" do -# # skip -# q = Queue.new -# q.enqueue(5) -# removed = q.dequeue -# removed.must_equal 5 -# q.empty?.must_equal true -# end + it "removes something from the Queue" do + # skip + q = Queue.new + q.enqueue(5) + removed = q.dequeue + removed.must_equal 5 + q.empty?.must_equal true + end -# it "removes the right something (LIFO)" do -# # skip -# q = Queue.new -# q.enqueue(5) -# q.enqueue(3) -# q.enqueue(7) -# removed = q.dequeue -# removed.must_equal 5 -# q.to_s.must_equal "[3, 7]" -# end + it "removes the right something (LIFO)" do + # skip + q = Queue.new + q.enqueue(5) + q.enqueue(3) + q.enqueue(7) + removed = q.dequeue + removed.must_equal 5 + q.to_s.must_equal "[3, 7]" + end -# it "properly adjusts the size with enqueueing and dequeueing" do -# # skip -# q = Queue.new -# q.empty?.must_equal true -# q.enqueue(-1) -# q.enqueue(-60) -# q.empty?.must_equal false -# q.size.must_equal 2 -# q.dequeue -# q.dequeue -# q.empty?.must_equal true -# end + it "properly adjusts the size with enqueueing and dequeueing" do + # skip + q = Queue.new + q.empty?.must_equal true + q.enqueue(-1) + q.enqueue(-60) + q.empty?.must_equal false + q.size.must_equal 2 + q.dequeue + q.dequeue + q.empty?.must_equal true + end -# it "returns the front element in the Queue" do -# # skip -# q = Queue.new -# q.enqueue(40) -# q.enqueue(22) -# q.enqueue(3) -# q.dequeue -# expect(q.dequeue).must_equal 22 -# end -# end + it "returns the front element in the Queue" do + # skip + q = Queue.new + q.enqueue(40) + q.enqueue(22) + q.enqueue(3) + q.dequeue + expect(q.dequeue).must_equal 22 + end +end diff --git a/test/stack_test.rb b/test/stack_test.rb index c58be71d..a29943f3 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -1,53 +1,53 @@ -# require "minitest/autorun" -# require "minitest/reporters" -# require_relative "../lib/stack" -# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/stack" +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# describe "Test Stack Implementation" do -# it "creates a Stack" do -# s = Stack.new -# s.class.must_equal Stack -# end +describe "Test Stack Implementation" do + it "creates a Stack" do + s = Stack.new + s.class.must_equal Stack + 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 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) -# s.push(30) -# s.to_s.must_equal "[10, 20, 30]" -# end + it "pushes multiple somethings onto a Stack" do + # skip + s = Stack.new + s.push(10) + s.push(20) + s.push(30) + s.to_s.must_equal "[10, 20, 30]" + end -# it "starts the stack empty" do -# # skip -# s = Stack.new -# s.empty?.must_equal true -# 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 -# removed.must_equal 5 -# s.empty?.must_equal true -# end + it "removes something from the stack" do + # skip + s = Stack.new + s.push(5) + removed = s.pop + removed.must_equal 5 + s.empty?.must_equal true + end -# it "removes the right something (LIFO)" do -# # skip -# s = Stack.new -# s.push(5) -# s.push(3) -# s.push(7) -# removed = s.pop -# removed.must_equal 7 -# s.to_s.must_equal "[5, 3]" -# end -# end + it "removes the right something (LIFO)" do + # skip + s = Stack.new + s.push(5) + s.push(3) + s.push(7) + removed = s.pop + removed.must_equal 7 + s.to_s.must_equal "[5, 3]" + end +end From a6442d32394eb3dde5d0d85d62c8d4d21eb420da Mon Sep 17 00:00:00 2001 From: Chu Date: Fri, 13 Sep 2019 21:06:52 -0700 Subject: [PATCH 6/6] added circular buffer --- lib/queue.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 61b35b53..89e33e13 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,83 @@ require_relative "./linked_list.rb" +# Using a circular buffer implement a Queue with the following methods: + +# enqueue(value) - Adds the value to the back of the queue. +# dequeue - removes and returns a value from the front of the queue +# empty? returns true if the queue is empty and false otherwise + class Queue + Q_SIZE = 5 + def initialize - @store = LinkedList.new + @store = Array.new(Q_SIZE) + @head = -1 + @tail = -1 end def enqueue(element) - @store.add_last(element) + # q is full if rear = length - 1 or head - 1 + if (@tail + 1 == @head) || (@tail + 1 == Q_SIZE) + raise ArugmentError, "queue is full" + # if head and tail are -1, then make it head + # move head and tail to 1 + elsif @head == -1 && @tail == -1 + @store[0] = element + @head = 0 + @tail = 0 + # head starts at not 0, and tail is last element + elsif @head > 0 && (@tail + 1 == Q_SIZE) + @tail = 0 + @store[@tail] = element + else + @tail += 1 + @store[@tail] = element + end end def dequeue - @store.remove_first + raise ArugmentError, "q empty" unless !empty? + # if head is at end of array + data = @store[@head] + @store[@head] = nil + if @head == Q_SIZE - 1 + # data = @store[@head] + # @store[@head] = nil + @head = 0 + # if only one element + elsif @head == @tail + # data = @store[@head] + # @store[@head] = nil + @head = @tail = -1 + else + # data = @store[@head] + # @store[@head] = nil + @head += 1 + end + return data end def front - return @store.head + return @head == -1 ? nil : store[@head] end def size - return @store.length + if empty? + return 0 + elsif @head == 0 + return @tail + 1 + elsif @tail < @head + return (Q_SIZE - head + @tail + 1) + end end def empty? - return @store.empty? + return @head == -1 && @tail == -1 end def to_s - return @store.to_s + clone_store = @store.clone + clone_store.delete(nil) + return clone_store.to_s end end