From 81a755fe907bf6d10d6edcf5c629b294a54302dc Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 8 Jun 2016 08:31:41 -0700 Subject: [PATCH] Finished linked-list homework --- array-list.rb | 37 +++++++++++++++++++++++++++---- linked-list.rb | 59 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/array-list.rb b/array-list.rb index a0d0342..f9bcca7 100644 --- a/array-list.rb +++ b/array-list.rb @@ -1,26 +1,55 @@ # Implementation of a list using a Native array --> ruby does this automatically -class ArrayList +class ArrayList # Ruby's array is an ArrayList. We are simulating what a Ruby array does by showing what happens with the native array within the Ruby array. + def initialize - @storage = [] + @storage = [nil,nil,nil,nil,nil] # this array has a capacity of 5 + @size = 0 # this is what we think is actually inside of this array end - def add(value) + def add(value) # this is simulating push (<<) + @storage[@size] = value + @size += 1 end - def delete(value) + # "deletes" last value + def delete + 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..118697a 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 @@ -40,6 +40,7 @@ def delete(val) # ... x->z # ( and z is basically y.next_node ) current = @head + ## wait what's happening in this next while loop? when would current be nil? while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val) current = current.next_node end @@ -63,9 +64,43 @@ def display end def include?(key) + + # Traverse through the list till you hit the "nil" at the end + current = @head + full_list = [] + while current.next_node != nil + if current.value == key + return true + else + full_list += [current.value.to_s] + current = current.next_node + end + end + + if current.value == key + return true + else + full_list += [current.value.to_s] + end + + return false + end def size + + current = @head + full_list = [] + size_of_list = 0 + while current.next_node != nil + full_list += [current.value.to_s] + size_of_list += 1 + current = current.next_node + end + full_list += [current.value.to_s] + size_of_list += 1 + return size_of_list + end def max @@ -84,10 +119,30 @@ def max puts "Displaying Linked List:" ll.display +puts "Does Linked List include the given value?" +puts "5:" +puts ll.include?(5) +puts "10:" +puts ll.include?(10) +puts "20:" +puts ll.include?(20) +puts "16:" +puts ll.include?(16) + +puts "Size of array:" +puts ll.size + puts "Delete 10 and then display the linked list:" ll.delete(10) ll.display +puts "Does it still include the given value?" +puts "10:" +puts ll.include?(10) + +puts "What is the size now?" +puts ll.size + =begin Output: Initialized a Node with value: 5