Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions array-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@

class ArrayList
def initialize
@storage = []
@storage = [nil,nil,nil,nil,nil]
@size = 0
end

def add(value)
def add(value) #like the push method in ruby.
@storage[@size] = value
@size += 1
end

def delete(value)
def delete#delete the last 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
Expand All @@ -37,5 +64,5 @@ def max
arr.display

puts "Delete 10 and then display the array list:"
arr.delete(10)
arr.delete
arr.display
51 changes: 48 additions & 3 deletions linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,14 +63,45 @@ def display
end

def include?(key)
current = @head
#while we have nodes to traverse
while current != nil
#when we find the key
return true if current.value == key
#reassign currrent
current = current.next_node
end
puts "The list does not include #{key}"
return false
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! this looks nice! the only thing is that you shouldn't "puts" anything because I did not ask for that, which means that you are creating unintended side-affects for the method that may not be expected.


def size
@size = 0
current = @head
while current != nil
#increase size
@size += 1
#reassign current
current = current.next_node
end
puts "This linked list has #{@size} elements."
return @size
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good too! Same comment as above, though.


def max
current = @head
max_value = current.value
while current != nil
#if the current node has a larger value than the max gets reasigned
if current.value > max_value
max_value = current.value
end
#if max_value is larger than keep moving to check next node
current = current.next_node
end
puts "#{max_value} is the max value element in the list."
return max_value
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Though I'm not sure what Ruby does in the case that you call .value on a nil object if there are no nodes in the list at all (@Head == nil). In other languages this causes an error because you can't call a method on nil. Therefore I don't know what max_value is in the case thta the list is empty. Regardless, you should no that this is something we should protect against happening in other languages.


end

# Initializing a Linked List with a node containing value (5)
Expand All @@ -79,14 +110,28 @@ def max
# Adding Elements to Linked List
ll.add(10)
ll.add(20)
# add more to list
ll.add(11)
ll.add(99)

# Display the Linked List
puts "Displaying Linked List:"
ll.display
ll.include?(5)
# add more to test include?
ll.include?(11)
ll.include?(98)
ll.size
ll.max


puts "Delete 10 and then display the linked list:"
ll.delete(10)
ll.display
ll.size
ll.max


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay! more tests!


=begin
Output:
Expand Down