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
@@ -1,26 +1,55 @@
# Implementation of a list using a Native array --> ruby does this automatically

class ArrayList
class ArrayList # Ruby's array is an ArrayList. We are simulating what a Ruby array does by showing what happens with the native array within the Ruby array.

def initialize
@storage = []
@storage = [nil,nil,nil,nil,nil] # this array has a capacity of 5
@size = 0 # this is what we think is actually inside of this array
end

def add(value)
def add(value) # this is simulating push (<<)
@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 Down
59 changes: 57 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 @@ -40,6 +40,7 @@ def delete(val)
# ... x->z
# ( and z is basically y.next_node )
current = @head
## wait what's happening in this next while loop? when would current be nil?
while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val)
current = current.next_node
end
Expand All @@ -63,9 +64,43 @@ def display
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
if current.value == key
return true
else
full_list += [current.value.to_s]
current = current.next_node
end
end

if current.value == key
return true
else
full_list += [current.value.to_s]
end

return false

end

Choose a reason for hiding this comment

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

This looks generally good except that we don't need any of the lines with full_list because we're just looking for a value in the list and then returning true or false if we find it or not


def size

current = @head
full_list = []
size_of_list = 0
while current.next_node != nil
full_list += [current.value.to_s]
size_of_list += 1
current = current.next_node
end
full_list += [current.value.to_s]
size_of_list += 1
return size_of_list

end

Choose a reason for hiding this comment

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

This looks good too -- except that we don't need full_list here either


def max
Expand All @@ -84,10 +119,30 @@ def max
puts "Displaying Linked List:"
ll.display

puts "Does Linked List include the given value?"
puts "5:"
puts ll.include?(5)
puts "10:"
puts ll.include?(10)
puts "20:"
puts ll.include?(20)
puts "16:"
puts ll.include?(16)

puts "Size of array:"
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

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

puts "Does it still include the given value?"
puts "10:"
puts ll.include?(10)

puts "What is the size now?"
puts ll.size

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