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
25 changes: 23 additions & 2 deletions array-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,48 @@

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

# Adds _value_ at the end of the list
def add(value)
@storage[@size] = value
@size += 1
end

# Deletes the _last_ value in the array
def delete
# delete the last value
@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
@size
end

def max
biggest = 0
@size.times do |i|
if @storage[i] > @storage[biggest]
biggest = i
end
end
return @storage[biggest]
end

end
Expand Down
117 changes: 100 additions & 17 deletions linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,52 @@ def delete(val)
end

def display
# Traverse through the list till you hit the "nil" at the end
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]
puts full_list.join("->")
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
full_list += [current.value.to_s]
current = current.next_node
end
full_list += [current.value.to_s]
puts full_list.join("->")
end

def size
end

def max
end
def include?(key)
current = @head
# breaks out when we reach last instead of check last
while current.next_node != nil
return true if current.value == key
current = current.next_node
end
return true if current.value == key
return false
end

def size
current = @head
size = 1
while current.next_node != nil
size += 1
current = current.next_node
end
size

Choose a reason for hiding this comment

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

I know ruby says it's ok not to put the return in front of this, but I think it's weird :)

end

def max
current = @head
max = [current]
while current.next_node != nil
current = current.next_node
if current.value > max[0].value
max[0] = current
end
end
max[0] = current if current.value > max[0].value
# i decided to return the node itself, and you can
# call value on it to see its value
max[0]
end

Choose a reason for hiding this comment

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

This looks pretty good, but I don't understand why max is an array of a single node. I think it would have made more sense to say max = current.value (assuming there is at least one node in the list) and then we wouldn't need the max[0] everywhere


end

Expand All @@ -84,17 +111,73 @@ def max
puts "Displaying Linked List:"
ll.display

puts "Shows the max:"
puts ll.max.value

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

puts "Shows the size:"
puts ll.size

puts "Shows the max:"
puts ll.max.value

ll.add(35)
ll.add(15)

puts "Displaying Linked List:"
ll.display

puts "Shows the max:"
puts ll.max.value

puts "Does it include 20?"
puts ll.include?(20)

puts "Does it include 35?"
puts ll.include?(35)

puts "Does it include 10?"
puts ll.include?(10)

puts "Does it include 5?"
puts ll.include?(5)

puts "Shows the size:"
puts ll.size


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:
Initialized a Node with value: 5
Initialized a Node with value: 10
Initialized a Node with value: 20
Displaying Linked List:
5->10->20
Shows the max:
20
Delete 10 and then display the linked list:
5->20
Shows the size:
2
Shows the max:
20
Initialized a Node with value: 35
Initialized a Node with value: 15
Displaying Linked List:
5->20->35->15
Shows the max:
35
Does it include 20?
true
Does it include 35?
true
Does it include 10?
false
Does it include 5?
true
Shows the size:
4
=end