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

class ArrayList
def initialize
@storage = []
@storage = [nil, nil, nil, nil, nil]
@size = 0
end

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

def delete(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

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

puts "Delete 10 and then display the array list:"
arr.delete(10)
# arr.delete(10)
arr.display
172 changes: 109 additions & 63 deletions linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,103 @@
# 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: " + 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)
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)
end

def size
end

def max
end
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

# the next 2 are pretty similar to display
def include?(key)
#include is check if its in there
current = @head
while current != nil
if current.value == key
return true
else
current = current.next_node
end
end
return false
end

def size
#how many nodes are in the list
current = @head
counter = 0
while current != nil
counter += 1
current = current.next_node
end
return counter
end

def max
current = @head
max = 0
while current != nil
if current.value > max
max = current.value
current = current.next_node
else
current = current.next_node
end
end
return max
end

end

Expand All @@ -79,14 +109,30 @@ def max
# Adding Elements to Linked List
ll.add(10)
ll.add(20)
ll.add(30)
ll.add(45)
ll.add(16)
ll.add(21)

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

puts "Delete 10 and then display the linked list:"
ll.delete(10)
ll.delete(21)
ll.display
puts "Does the list include 5?"
puts ll.include?(5)
puts "Does the list include 10?"
puts ll.include?(10)
puts "Does the list include 20?"
puts ll.include?(20)
puts "What is the size of the list?"
puts ll.size
puts "What is the max value?"
puts ll.max


=begin
Output:
Expand Down