From a7b8f1ce56e1cd95c658213cab64c6139ec56bc9 Mon Sep 17 00:00:00 2001 From: Jade Vance Date: Sat, 28 May 2016 18:34:33 -0700 Subject: [PATCH 1/2] homework for native arrays and linked lists --- array-list.rb | 28 ++++++++++++++++++++++++---- linked-list.rb | 6 ++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/array-list.rb b/array-list.rb index a0d0342..e27a02b 100644 --- a/array-list.rb +++ b/array-list.rb @@ -2,27 +2,47 @@ class ArrayList def initialize - @storage = [] + @storage = [nil,nil,nil,nil,nil,nil,nil,nil,nil,nil] + @size = 0 end def add(value) + @storage[@size] = value + @size += 1 end - def delete(value) + def delete #last value + @size -= 1 end def display + @size.times do |index| + puts @storage[index] + end end - def include?(key) + def include?(value) + @size.times do |index| + if @storage[index] == value + return true + end + end + return false end def size + return @size end def max + max = 0 + @size.times do |index| + if @storage[index] > @storage[max] + max = index + end + end + return @storage[max] end - end # Initializing an Array List diff --git a/linked-list.rb b/linked-list.rb index a1b30bd..5d8b298 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 @@ -98,3 +98,5 @@ def max Delete 10 and then display the linked list: 5->20 =end + +# Homework: write include/size/and max From cf0ec034ac8d9375f9ca38283600fa7123c920cb Mon Sep 17 00:00:00 2001 From: Jade Vance Date: Sun, 29 May 2016 12:37:12 -0700 Subject: [PATCH 2/2] adding in linked-list file, forgot to commit it yesterday. --- linked-list.rb | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/linked-list.rb b/linked-list.rb index 5d8b298..2c6fec6 100644 --- a/linked-list.rb +++ b/linked-list.rb @@ -62,13 +62,35 @@ def display puts full_list.join("->") end - def include?(key) + def include?(value) + current = @head + while !(current.next_node == nil) + return true if current.value == value + current = current.next_node + end + return true if current.value == value + return false end def size + current = @head + size = 0 + while !(current.next_node == nil) + size += 1 + current = current.next_node + end + size += 1 end def max + current = @head + max = current.value + while !(current.next_node == nil) + max = current.value if current.value > max + current = current.next_node + end + max = current.value if current.value > max + max end end @@ -87,7 +109,11 @@ def max puts "Delete 10 and then display the linked list:" ll.delete(10) ll.display - +puts ll.size +puts ll.include?(5) +puts ll.include?(10) +puts ll.include?(20) +puts ll.max =begin Output: Initialized a Node with value: 5