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
31 changes: 26 additions & 5 deletions array-list.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
# Implementation of a list using a Native array --> ruby does this automatically

# we are trying to simulate what a ruby array does by simulating a native array under the hood
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)
def delete #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
return @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 All @@ -36,6 +57,6 @@ def max
puts "Displaying Array List:"
arr.display

puts "Delete 10 and then display the array list:"
arr.delete(10)
puts "Delete last element and then display the array list:"
arr.delete
arr.display
27 changes: 25 additions & 2 deletions linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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

class LinkedList
attr_accessor :head
def initialize(val)
# Initialize a new node at the head
@head = Node.new(val,nil)
Expand Down Expand Up @@ -63,12 +64,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
return true if current.value == key
return false
end

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

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 all of these (include?, size, and max) 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
current = @head
max = current.value
while current.next_node != nil
max = current.value if current.value > max
current = current.next_node
end
max = current.value if current.value > max
max
end

end
Expand Down