From 491c3c12a9b19fac52a217b9120843e483990667 Mon Sep 17 00:00:00 2001 From: Anna Wilson Date: Fri, 27 May 2016 22:28:11 -0700 Subject: [PATCH] commit --- array-list.rb | 33 ++++++++++++++++++++++++++++++--- linked-list.rb | 32 +++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/array-list.rb b/array-list.rb index a0d0342..7b7e9cb 100644 --- a/array-list.rb +++ b/array-list.rb @@ -2,25 +2,52 @@ class ArrayList def initialize - @storage = [] + @storage = [nil,nil,nil,nil,nil] #storage capacity + #this array has a capacity of 5, but nothing is in it, so the size is 0 + #sample = [] + #[].size = 0, however ruby on the down low created [nil,nil,nil,nil] just to save memory for it + @size = 0 #this is what we think is actually in the array end - def add(value) + def add(value) #add to the end + @storage[@size] = value + @size += 1 end def delete(value) + #@storage[@size] = nil + @size -= 1 end - def display + def display #print out all the values that are actually a thing, so only up to size. + @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 + return @size end def max + return nil if empty? + + 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 a1b30bd..2232e47 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 @@ -21,7 +21,7 @@ def initialize(val) def add(value) # Traverse to the end of the list # And insert a new node over there with the specified value - current = @head + current = @head #keep track of head while current.next_node != nil current = current.next_node end @@ -63,9 +63,34 @@ def display end def include?(key) + current = @head + + while current.next_node != nil + return true if current.value == key + current = current.next_node + end + + if current.next_node == nil + return true if current.value == key + end + + return false end def size + current = @head + size = 0 + + while current.next_node != nil + size += 1 + current = current.next_node + end + + if current.next_node == nil + size += 1 + end + + size end def max @@ -88,6 +113,7 @@ def max ll.delete(10) ll.display + =begin Output: Initialized a Node with value: 5