From b8ee52625ddbff69bbe13b5c5819f39600135b56 Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Sun, 29 May 2016 21:34:28 -0700 Subject: [PATCH] did methods for size and include --- array-list.rb | 39 ++++++++++++++++++++++++++++++++++----- linked-list.rb | 36 ++++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/array-list.rb b/array-list.rb index a0d0342..eff0b26 100644 --- a/array-list.rb +++ b/array-list.rb @@ -2,25 +2,54 @@ class ArrayList def initialize - @storage = [] + @storage = [0,0,0,0,0] + @size = 0 end def add(value) + @storage[@size] = value + @size += 1 end - def delete(value) + #deletes the last value + def delete + return nil if empty? + @size -= 1 end - def display + def display #print out all values in this array and 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 #return false should not be inside the loop bc it will return false hte first time it doesnt find that value. 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 + + def empty? + size == 0 end end @@ -36,6 +65,6 @@ def max puts "Displaying Array List:" arr.display -puts "Delete 10 and then display the array list:" -arr.delete(10) +puts "Delete last element and then display the array list:" +arr.delete arr.display diff --git a/linked-list.rb b/linked-list.rb index a1b30bd..157ac70 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 #temp, move the pointer current but not the pointer head while current.next_node != nil current = current.next_node end @@ -55,21 +55,37 @@ def display current = @head full_list = [] while current.next_node != nil - full_list += [current.value.to_s] + full_list += [current.value.to_s] #try to figure this out first.....slack crystal if you need hep current = current.next_node end full_list += [current.value.to_s] puts full_list.join("->") end - def include?(key) - end + def include?(key) #do for homework + current = @head + while current != nil + return true if current.value == key + current = current.next_node + end + return false + end - def size - end - def max - end + def size #do for homework + @size = 0 + current = @head + while current != nil + @size += 1 + current = current.next_node + end + return @size + end + + + def max #do if feeling ambitious + + end end