From a1d1d7caf221c326438b9ffb7d58e502aaeefd5b Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Sun, 29 May 2016 18:56:40 -0700 Subject: [PATCH] homework --- array-list.rb | 29 ++++++++++++++++++++++++++++- linked-list.rb | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/array-list.rb b/array-list.rb index a0d0342..401bd36 100644 --- a/array-list.rb +++ b/array-list.rb @@ -2,25 +2,52 @@ class ArrayList def initialize - @storage = [] + @storage = [nil,nil,nil,nil,nil] + @size = 0 end def add(value) + @storage[@size] = value + @size += 1 end def delete(value) + return nil if empty? + @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 + return @storage[biggest] + end + + def empty? + @size == 0 end end diff --git a/linked-list.rb b/linked-list.rb index a1b30bd..387e97f 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,9 +63,26 @@ def display end def include?(key) + current = @head + full_list = [] + return p "#{key} is included!" if current.value == key + while current.next_node != nil + current = current.next_node + return p "#{key} is included!" if current.value == key + end + return p "#{key} is not included." + end def size + current = @head + full_list = [] + while current.next_node != nil + full_list += [current.value.to_s] + current = current.next_node + end + full_list += [current.value.to_s] + return p "Size is #{full_list.length}." end def max @@ -88,6 +105,17 @@ def max ll.delete(10) ll.display +puts "Does Linked List include 20?" +ll.include?(20) + +puts "Does Linked List include 5?" +ll.include?(5) + +puts "Does Linked List include 10?" +ll.include?(10) + +puts "What is the size of the Linked List?" +ll.size =begin Output: Initialized a Node with value: 5