Class: Mores::LinkedList
- Inherits:
-
Object
- Object
- Mores::LinkedList
- Includes:
- Enumerable
- Defined in:
- lib/mores/linked_list.rb
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#length ⇒ Object
(also: #size)
readonly
Returns the value of attribute length.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #>>(value) ⇒ Object
- #clear ⇒ Object
- #count(*args) ⇒ Object
- #delete(value) {|value| ... } ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object
- #each_node ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ LinkedList
constructor
A new instance of LinkedList.
- #reverse_each ⇒ Object
- #reverse_each_node ⇒ Object
Constructor Details
#initialize ⇒ LinkedList
Returns a new instance of LinkedList.
12 13 14 |
# File 'lib/mores/linked_list.rb', line 12 def initialize @length = 0 end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
9 10 11 |
# File 'lib/mores/linked_list.rb', line 9 def head @head end |
#length ⇒ Object (readonly) Also known as: size
Returns the value of attribute length.
9 10 11 |
# File 'lib/mores/linked_list.rb', line 9 def length @length end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
9 10 11 |
# File 'lib/mores/linked_list.rb', line 9 def tail @tail end |
Instance Method Details
#<<(value) ⇒ Object
55 56 57 |
# File 'lib/mores/linked_list.rb', line 55 def <<(value) insert nil, @head, value end |
#>>(value) ⇒ Object
59 60 61 |
# File 'lib/mores/linked_list.rb', line 59 def >>(value) insert @tail, nil, value end |
#clear ⇒ Object
63 64 65 66 67 68 |
# File 'lib/mores/linked_list.rb', line 63 def clear each_node { |n| n.send :clear } @head = @tail = nil @length = 0 self end |
#count(*args) ⇒ Object
20 21 22 23 |
# File 'lib/mores/linked_list.rb', line 20 def count(*args) return @length if args.empty? && !block_given? super end |
#delete(value) {|value| ... } ⇒ Object
70 71 72 73 74 |
# File 'lib/mores/linked_list.rb', line 70 def delete(value) last = each_node.reduce(NOMATCH) { |v, n| n.value == value ? n.delete : v } return last unless last == NOMATCH yield value if block_given? end |
#delete_if ⇒ Object
76 77 78 79 |
# File 'lib/mores/linked_list.rb', line 76 def delete_if return to_enum(__callee__) { @length } unless block_given? each_node { |n| n.delete if yield n.value } end |
#each ⇒ Object
25 26 27 28 |
# File 'lib/mores/linked_list.rb', line 25 def each return to_enum(__callee__) { @length } unless block_given? each_node { |n| yield n.value } end |
#each_node ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/mores/linked_list.rb', line 35 def each_node return to_enum(__callee__) { @length } unless block_given? node = @head until node.nil? node = (cur = node).next yield cur end self end |
#empty? ⇒ Boolean
16 17 18 |
# File 'lib/mores/linked_list.rb', line 16 def empty? !@head end |
#reverse_each ⇒ Object
30 31 32 33 |
# File 'lib/mores/linked_list.rb', line 30 def reverse_each return to_enum(__callee__) { @length } unless block_given? reverse_each_node { |n| yield n.value } end |
#reverse_each_node ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/mores/linked_list.rb', line 45 def reverse_each_node return to_enum(__callee__) { @length } unless block_given? node = @tail until node.nil? node = (cur = node).prev yield cur end self end |