Class: Algorithmable::DataStructs::LinkedList
- Inherits:
-
Object
- Object
- Algorithmable::DataStructs::LinkedList
- Includes:
- Enumerable
- Defined in:
- lib/algorithmable/data_structs/linked_list.rb
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #first ⇒ Object
-
#initialize ⇒ LinkedList
constructor
A new instance of LinkedList.
- #last ⇒ Object
- #push(data) ⇒ Object
- #unshift(data) ⇒ Object
Constructor Details
#initialize ⇒ LinkedList
Returns a new instance of LinkedList.
7 8 9 10 11 12 |
# File 'lib/algorithmable/data_structs/linked_list.rb', line 7 def initialize @head = nil @tail = nil @length = 0 @op_counter = 0 end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
5 6 7 |
# File 'lib/algorithmable/data_structs/linked_list.rb', line 5 def length @length end |
Instance Method Details
#each(&block) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/algorithmable/data_structs/linked_list.rb', line 30 def each(&block) next_node = @head nodes = [] while next_node nodes << next_node next_node = next_node.succ end nodes.each(&block) end |
#first ⇒ Object
22 23 24 |
# File 'lib/algorithmable/data_structs/linked_list.rb', line 22 def first @head ? @head.dup : fail_index_error end |
#last ⇒ Object
26 27 28 |
# File 'lib/algorithmable/data_structs/linked_list.rb', line 26 def last @tail ? @tail.dup : fail_index_error end |
#push(data) ⇒ Object
18 19 20 |
# File 'lib/algorithmable/data_structs/linked_list.rb', line 18 def push(data) link_tail_node data end |
#unshift(data) ⇒ Object
14 15 16 |
# File 'lib/algorithmable/data_structs/linked_list.rb', line 14 def unshift(data) link_head_node data end |