From 42d197c01561e1490f5562b9c2150c422eb9699e Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Mon, 6 Jun 2016 08:57:52 -0700 Subject: [PATCH] homework --- array-list.rb | 34 ++++++++++++++++++++++++++++++---- linked-list.rb | 28 ++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/array-list.rb b/array-list.rb index a0d0342..6129294 100644 --- a/array-list.rb +++ b/array-list.rb @@ -2,27 +2,53 @@ 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 + @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 + return @size end def max + return nil if empty? + biggest = 0 + @size.times do |i| + if @storage[i] > @storage[biggest] + biggest = i + end + end + returns @storage[biggest] end + def empty? + @size == 0 + end end # Initializing an Array List @@ -36,6 +62,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..5edf9bd 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 + while current != nil + if current.value == key + return true + end + current = current.next_node + end + return false end def size + size = 0 + current = @head + while current != nil + size += 1 + current = current.next_node + end + return size end def max + current_max = 0 + current = @head + while current != nil + if current.value > current_max + current_max = current.value + end + current = current.next_node + end + return current_max end end