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

class ArrayList
def initialize
@storage = []
@storage = [nil, nil, nil, nil, nil]
# size number of items in array, not necessarily the capacity
@size = 0
end

def add(value)
@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
Expand All @@ -36,6 +65,6 @@ def max
puts "Displaying Array List:"
arr.display

puts "Delete 10 and then display the array list:"
arr.delete(10)
puts "Delete last and then display the array list:"
arr.delete
arr.display
63 changes: 50 additions & 13 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 @@ -55,20 +55,44 @@ def display
current = @head
full_list = []
while current.next_node != nil
full_list += [current.value.to_s]
full_list << current.value.to_s
current = current.next_node
end
full_list += [current.value.to_s]
puts full_list.join("->")
end

# doing while current.next_node != nil prevent code from ever looking
# at the last node, so they wouldn't get evaluated using the pattern above
def include?(key)
current = @head
while current != nil
return true if current.value == key
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 = @head
maximum = current.value
while current != nil
if current.value > maximum
maximum = current.value
end
current = current.next_node
end
return maximum
end

Choose a reason for hiding this comment

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

I LOVE that you recognized that you could change your loop to while current != nil instead of while current.next_node != nil ! I told you to model after display which does it the latter way -- because sometimes you need to do something different with the last case and I wanted that to be the pattern I showed -- but now I want to point out how we could have optimized the pattern just slightly. Really nice!


end
Expand All @@ -80,6 +104,18 @@ def max
ll.add(10)
ll.add(20)

# Crystal - I added a few more to test my max method
ll.add(65)
ll.add(2)
ll.add(6)

# Crystal - these were my tests for include?, size, and max methods
puts ll.include?(10) # true (passes)
puts ll.include?(4) # false (passes)
puts ll.size # 6 (passes)
puts ll.max # 65 (passes)


# Display the Linked List
puts "Displaying Linked List:"
ll.display
Expand All @@ -88,13 +124,14 @@ def max
ll.delete(10)
ll.display

=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
Delete 10 and then display the linked list:
5->20
=end


# 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
# Delete 10 and then display the linked list:
# 5->20