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
29 changes: 28 additions & 1 deletion 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)
@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
Expand Down
32 changes: 30 additions & 2 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,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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These looks generally good -- however, both methods are creating a side effect of printing something. If you are not asked to print something out, then you don't necessarily want to because it limits how you can use that method in the future.

I would expect that a method with a ? at the end would return a boolean and not print anything out.

I would also expect that a size method would only return a single numerical value and nothing else.


def max
Expand All @@ -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
Expand Down