From d94c3f4092e5636f340c70693554ef57f2fd08e0 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sun, 29 May 2016 21:30:05 -0700 Subject: [PATCH 1/2] hw methods --- array-list.rb | 21 ++++++++++++++++++++- linked-list.rb | 28 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/array-list.rb b/array-list.rb index a0d0342..8ceb5cb 100644 --- a/array-list.rb +++ b/array-list.rb @@ -2,25 +2,44 @@ class ArrayList def initialize - @storage = [] + @storage = [nil, nil, nil, nil, nil] + @size = 0 end def add(value) + @storage[@size] = value + @storage += 1 end def delete(value) + @size -= 1 end def display + return @storage[0, @size] 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] > biggest + biggest = i + end + end + end end diff --git a/linked-list.rb b/linked-list.rb index a1b30bd..cd12221 100644 --- a/linked-list.rb +++ b/linked-list.rb @@ -5,9 +5,9 @@ class Node attr_accessor :value, :next_node - def initialize(val,next_in_line=null) + def initialize(val,next_in_line = nil) @value = val - @next_nodex = next_in_line + @next_node = next_in_line puts "Initialized a Node with value: " + value.to_s end end @@ -63,12 +63,36 @@ def display end def include?(key) + current = @head + full_list = [] + while current.next_node != nil + full_list += [current.value.to_s] + (return true) if current.value == key + current = current.next_node + end + return false end def size + current = @head + count = 0 + full_list = [] + while current.next_node != nil + full_list += [current.value.to_s] #not necessary but nice + current = current.next_node + count += 1 + end + return count end def max + current = @head + max = 0 + while current.next_node != nil + (max += current.value) if current.value > max + current = current.next_node + end + return max end end From ba6c057d629b3a336e859ef2532d0a89dc163c92 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sun, 29 May 2016 21:35:18 -0700 Subject: [PATCH 2/2] forgot how to count for a second there --- linked-list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linked-list.rb b/linked-list.rb index cd12221..ad0a484 100644 --- a/linked-list.rb +++ b/linked-list.rb @@ -75,7 +75,7 @@ def include?(key) def size current = @head - count = 0 + count = 1 full_list = [] while current.next_node != nil full_list += [current.value.to_s] #not necessary but nice