Class: LinkedList
- Inherits:
-
Object
show all
- Defined in:
- lib/honey_mushroom/linked_list.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of LinkedList.
3
4
5
|
# File 'lib/honey_mushroom/linked_list.rb', line 3
def initialize
@head = nil
end
|
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head.
2
3
4
|
# File 'lib/honey_mushroom/linked_list.rb', line 2
def head
@head
end
|
Instance Method Details
#add(value) ⇒ Object
7
8
9
10
11
|
# File 'lib/honey_mushroom/linked_list.rb', line 7
def add(value)
node = Node.new({value: value, next: nil})
node.next = @head
@head = node
end
|
#find(value) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/honey_mushroom/linked_list.rb', line 20
def find(value)
current = @head
until current.value == value
current = current.next
end
return "Value: #{current.value} Next: #{current.next.value} ID: #{current.id}"
end
|
#remove_front ⇒ Object
13
14
15
16
17
18
|
# File 'lib/honey_mushroom/linked_list.rb', line 13
def remove_front
current = @head
@head = current.next
return current
end
|