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
39 changes: 34 additions & 5 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 = [0,0,0,0,0]
@size = 0
end

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

def delete(value)
#deletes the last value
def delete
return nil if empty?
@size -= 1
end

def display
def display #print out all values in this array and 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 #return false should not be inside the loop bc it will return false hte first time it doesnt find that value.
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 element and then display the array list:"
arr.delete
arr.display
36 changes: 26 additions & 10 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 #temp, move the pointer current but not the pointer head
while current.next_node != nil
current = current.next_node
end
Expand Down Expand Up @@ -55,21 +55,37 @@ def display
current = @head
full_list = []
while current.next_node != nil
full_list += [current.value.to_s]
full_list += [current.value.to_s] #try to figure this out first.....slack crystal if you need hep
current = current.next_node
end
full_list += [current.value.to_s]
puts full_list.join("->")
end

def include?(key)
end
def include?(key) #do for homework
current = @head
while current != nil
return true if current.value == key
current = current.next_node
end
return false
end

def size
end

def max
end
def size #do for homework
@size = 0
current = @head
while current != nil
@size += 1
current = current.next_node
end
return @size
end


def max #do if feeling ambitious

end

end

Expand Down