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: 37 additions & 2 deletions array-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,62 @@

class ArrayList
def initialize
@storage = []
#This array has the capacity of five. When we tell Ruby Array.new (or we just throw up some brackets) Ruby goes and reserves space in memory for our Array. That is what we are trying to illustrate.
# storage = capacity (what _could_ it hold)
@storage = [nil, nil, nil, nil, nil]
@size = 0
end

def add(value)
#We are putting the value in the index that is equal to the size of the array. If the size is 0, the value goes into the zero index position.
@storage[@size] = value
@size += 1
end

def delete(value)
return nil if empty?
@size -= 1

#We could do this, but we don't need to. @storage will always have 5 spots in it, we can just adjust the @size to reflect what we want to be in the array. We COULD delete the very last value, but don't NEED to.
# @storage[@size] = nil
end

def display
#Linearly we are printing each value within storage.
@size.times do |i|
puts @storage[i]
end
end

def include?(key)
@size.times do |i|
if @storage[i] = key
return true
end
end
#This false is not, and should not be a part of the above if loop as an else. If it _were_ the loop would stop once it met the first not "key" value in the array.
return false
end

def size
return @size
end

def max
# We start by assuming that the biggest value is 0. As we go through the array we check to see if each value is bigger that 0. If the value being passed through the loop is greater, then the value of biggest is replaced by the value.
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

# Initializing an Array List
Expand All @@ -37,5 +72,5 @@ def max
arr.display

puts "Delete 10 and then display the array list:"
arr.delete(10)
arr.delete
arr.display
164 changes: 103 additions & 61 deletions linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,102 @@
# LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list

class Node
attr_accessor :value, :next_node
attr_accessor :value, :next_node

def initialize(val,next_in_line=null)
@value = val
@next_nodex = next_in_line
puts "Initialized a Node with value: " + value.to_s
end
def initialize(val,next_in_line=nil)
@value = val
@next_node = next_in_line
puts "Initialized a Node with value: " + val.to_s
end
end

class LinkedList
def initialize(val)
# Initialize a new node at the head
@head = Node.new(val,nil)
end

def add(value)
# Traverse to the end of the list
# And insert a new node over there with the specified value
current = @head
while current.next_node != nil
current = current.next_node
end
current.next_node = Node.new(value,nil)
self
end

def delete(val)
current = @head
if current.value == val
# If the head is the element to be delete, the head needs to be updated
@head = @head.next_node
else
# ... x -> y -> z
# Suppose y is the value to be deleted, you need to reshape the above list to :
# ... x->z
# ( and z is basically y.next_node )
current = @head
while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val)
current = current.next_node
end

if (current != nil) && (current.next_node != nil)
current.next_node = (current.next_node).next_node
end
end
#In a LinkedList each node only knows what the node in front of it is, it does not know what the node behind it was, or what the last node is. It only knows about the next node.
def initialize(val)
# Initialize a new node at the head
@head = Node.new(val,nil)
end

def add(value)
# Traverse to the end of the list
# And insert a new node over there with the specified value
current = @head
while current.next_node != nil
current = current.next_node
end
current.next_node = Node.new(value,nil)
self
end

def delete(val)
current = @head
if current.value == val
# If the head is the element to be delete, the head needs to be updated
@head = @head.next_node
else
# ... x -> y -> z
# Suppose y is the value to be deleted, you need to reshape the above list to :
# ... x->z
# ( and z is basically y.next_node )
current = @head
while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val)
current = current.next_node
end

if (current != nil) && (current.next_node != nil)
current.next_node = (current.next_node).next_node
end
end
end

def display
# Traverse through the list till you hit the "nil" at the end
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]
puts full_list.join("->")
end

def include?(key)
current = @head
while current != nil
if current.value != key
current = current.next_node
elsif current.value == key
puts "true"
return true
else
puts "false"
return false
end
end

def display
# Traverse through the list till you hit the "nil" at the end
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]
puts full_list.join("->")
end

def size
count = 1
current = @head
while current.next_node != nil
count += 1
current = current.next_node
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.

What will get returned here if the LinkedList has 0 nodes?


def include?(key)
return count
end

def max
max = 0
current = @head
while current != nil
if current.value > max
max = current.value
end
current = current.next_node
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What will happen here if my list contains -5 --> -10 --> -1 --> nil

end

def size
end

def max
end

return max
end
end

# Initializing a Linked List with a node containing value (5)
Expand All @@ -88,6 +116,16 @@ def max
ll.delete(10)
ll.display

# Find element in list:
puts "Is 20 in the list?"
"#{ll.include?(20)}"

# Give the size of the list:
puts "The size of the list is #{ll.size}."

#Find the highest value in the list:
puts "The highest value in the list is #{ll.max}."

=begin
Output:
Initialized a Node with value: 5
Expand All @@ -97,4 +135,8 @@ def max
5->10->20
Delete 10 and then display the linked list:
5->20
Is 20 in the list?
true
The size of the list is 2.
The highest value in the list is 20.
=end