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
33 changes: 30 additions & 3 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] #storage capacity
#this array has a capacity of 5, but nothing is in it, so the size is 0
#sample = []
#[].size = 0, however ruby on the down low created [nil,nil,nil,nil] just to save memory for it
@size = 0 #this is what we think is actually in the array
end

def add(value)
def add(value) #add to the end
@storage[@size] = value
@size += 1
end

def delete(value)
#@storage[@size] = nil
@size -= 1
end

def display
def display #print out all the values that are actually a thing, so only up to size.
@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

end
Expand Down
32 changes: 29 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 All @@ -21,7 +21,7 @@ def initialize(val)
def add(value)
# Traverse to the end of the list
# And insert a new node over there with the specified value
current = @head
current = @head #keep track of head
while current.next_node != nil
current = current.next_node
end
Expand Down Expand Up @@ -63,9 +63,34 @@ def display
end

def include?(key)
current = @head

while current.next_node != nil
return true if current.value == key
current = current.next_node
end

if current.next_node == nil
return true if current.value == key
end

return false
end

def size
current = @head
size = 0

while current.next_node != nil
size += 1
current = current.next_node
end

if current.next_node == nil
size += 1
end

size
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.

This looks great and works, but in both of these (include?, size) you had to account for that last node separately -- if you change your loop to while current != nil instead of while current.next_node != nil then you wouldn't need that! 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. Hopefully that makes sense!


def max
Expand All @@ -88,6 +113,7 @@ def max
ll.delete(10)
ll.display


=begin
Output:
Initialized a Node with value: 5
Expand Down