Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
34 changes: 30 additions & 4 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 = [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
@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
returns @storage[biggest]
end

def empty?
@size == 0
end
end

# Initializing an Array List
Expand All @@ -36,6 +62,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
28 changes: 26 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 @@ -63,12 +63,36 @@ def display
end

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

Choose a reason for hiding this comment

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

👍


def size
size = 0
current = @head
while current != nil
size += 1
current = current.next_node
end
return size

Choose a reason for hiding this comment

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

👍

end

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

Choose a reason for hiding this comment

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

👍


end
Expand Down