diff --git a/array-list.rb b/array-list.rb index 9c2041f..6c76a84 100644 --- a/array-list.rb +++ b/array-list.rb @@ -2,27 +2,48 @@ class ArrayList def initialize - @storage = [] + @storage = [nil,nil,nil,nil,nil] + @size = 0 end # Adds _value_ at the end of the list def add(value) + @storage[@size] = value + @size += 1 end - # Deletes the _last_ value in the array def delete + # delete the last value + @size -= 1 end def display + @size.times do |i| + puts @storage[i] + end end def include?(key) + @size.times do |i| + if @storage[i] == key + return true + end + end + return false end def size + @size end def max + biggest = 0 + @size.times do |i| + if @storage[i] > @storage[biggest] + biggest = i + end + end + return @storage[biggest] end end diff --git a/linked-list.rb b/linked-list.rb index cc2fe17..e086301 100644 --- a/linked-list.rb +++ b/linked-list.rb @@ -51,25 +51,52 @@ def delete(val) end def display - # Traverse through the list till you hit the "nil" at the end - current = @head - full_list = [] - while current.next_node != nil - full_list += [current.value.to_s] - current = current.next_node - end - full_list += [current.value.to_s] - puts full_list.join("->") - end - - def include?(key) + # Traverse through the list till you hit the "nil" at the end + current = @head + full_list = [] + while current.next_node != nil + full_list += [current.value.to_s] + current = current.next_node + end + full_list += [current.value.to_s] + puts full_list.join("->") end - def size - end - - def max - end + def include?(key) + current = @head + # breaks out when we reach last instead of check last + while current.next_node != nil + return true if current.value == key + current = current.next_node + end + return true if current.value == key + return false + end + + def size + current = @head + size = 1 + while current.next_node != nil + size += 1 + current = current.next_node + end + size + end + + def max + current = @head + max = [current] + while current.next_node != nil + current = current.next_node + if current.value > max[0].value + max[0] = current + end + end + max[0] = current if current.value > max[0].value + # i decided to return the node itself, and you can + # call value on it to see its value + max[0] + end end @@ -84,10 +111,44 @@ def max puts "Displaying Linked List:" ll.display +puts "Shows the max:" +puts ll.max.value + puts "Delete 10 and then display the linked list:" ll.delete(10) ll.display +puts "Shows the size:" +puts ll.size + +puts "Shows the max:" +puts ll.max.value + +ll.add(35) +ll.add(15) + +puts "Displaying Linked List:" +ll.display + +puts "Shows the max:" +puts ll.max.value + +puts "Does it include 20?" +puts ll.include?(20) + +puts "Does it include 35?" +puts ll.include?(35) + +puts "Does it include 10?" +puts ll.include?(10) + +puts "Does it include 5?" +puts ll.include?(5) + +puts "Shows the size:" +puts ll.size + + =begin Output: Initialized a Node with value: 5 @@ -95,6 +156,28 @@ def max Initialized a Node with value: 20 Displaying Linked List: 5->10->20 +Shows the max: +20 Delete 10 and then display the linked list: 5->20 +Shows the size: +2 +Shows the max: +20 +Initialized a Node with value: 35 +Initialized a Node with value: 15 +Displaying Linked List: +5->20->35->15 +Shows the max: +35 +Does it include 20? +true +Does it include 35? +true +Does it include 10? +false +Does it include 5? +true +Shows the size: +4 =end