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
32 changes: 30 additions & 2 deletions array-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,53 @@

class ArrayList
def initialize
@storage = []
@storage = [nil,nil,nil,nil,nil]
@size = 0
# capacity is 5 - but content is 0. there isn't anything occupying the available memory.
end

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

def delete(value)
# always deletes last value in collection
def delete
@size -= 1
# ignore the last spot/ decrease the size by 1/ "ignore this item"
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
return false
end

def size
@size
end

def max
return nil if empty?
biggest = 0
@size.times do |i|
if @storage[i] > @storage[biggest]
biggest = i
end
end
@storage[biggest]
end

def empty?
@size == 0
end

end
Expand Down
217 changes: 123 additions & 94 deletions linked-list.rb
Original file line number Diff line number Diff line change
@@ -1,100 +1,129 @@
# Quick Example of a Self Referential Data Structure in Ruby
# NODE -> contains a value and a pointer to (next_node)
# LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list

class 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
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)
# Quick Example of a Self Referential Data Structure in Ruby
# NODE -> contains a value and a pointer to (next_node)
# LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list

class Node
attr_accessor :value, :next_node

def initialize(val,next_in_line = nil)
@value = val
@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)
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
until current == nil
if current.value == key
return true
else
current = current.next_node
end
end
false

Choose a reason for hiding this comment

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

I know ruby says this is ok, but i think it's weird to leave off the return

end

def size
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
acc = 0
until current == nil
current = current.next_node
acc += 1
end
end
acc

Choose a reason for hiding this comment

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

ditto here

end

def display
# Traverse through the list till you hit the "nil" at the end
def max
current = @head
full_list = []
while current.next_node != nil
full_list += [current.value.to_s]
max = 0
until current == nil
if current.value > max
max = current.value
current = current.next_node
else
current = current.next_node
end
end
full_list += [current.value.to_s]
puts full_list.join("->")
end

def include?(key)
end

def size
end

def max
end

end

# Initializing a Linked List with a node containing value (5)
ll = LinkedList.new(5)

# Adding Elements to Linked List
ll.add(10)
ll.add(20)

# Display the Linked List
puts "Displaying Linked List:"
ll.display

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

=begin
Output:
Initialized a Node with value: 5
Initialized a Node with value: 10
Initialized a Node with value: 20
Displaying Linked List:
5->10->20
Delete 10 and then display the linked list:
5->20
=end
max
end

end

# Initializing a Linked List with a node containing value (5)
# ll = LinkedList.new(5)
#
# # Adding Elements to Linked List
# ll = LinkedList.new(5)
# ll.add(10)
# ll.add(20)
#
# # Display the Linked List
# puts "Displaying Linked List:"
# ll.display
#
# puts "Delete 10 and then display the linked list:"
# ll.delete(10)
# ll.display

# =begin
# Output:
# Initialized a Node with value: 5
# Initialized a Node with value: 10
# Initialized a Node with value: 20
# Displaying Linked List:
# 5->10->20
# Delete 10 and then display the linked list:
# 5->20
# =end