Class: DobLinkedList
- Inherits:
-
Object
- Object
- DobLinkedList
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/DobLinkedList.rb
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #[](index) ⇒ Object
- #each ⇒ Object
- #find_first(&predicate) ⇒ Object
- #find_node(index) ⇒ Object
-
#initialize ⇒ DobLinkedList
constructor
A new instance of DobLinkedList.
- #insertHead(value) ⇒ Object
- #insertMany(valuesArray) ⇒ Object
- #insertTail(value) ⇒ Object
- #remove(node) ⇒ Object
- #remove_head ⇒ Object
- #remove_tail ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ DobLinkedList
Returns a new instance of DobLinkedList.
28 29 30 31 32 |
# File 'lib/DobLinkedList.rb', line 28 def initialize @head = nil @tail = nil @length = 0 end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
26 27 28 |
# File 'lib/DobLinkedList.rb', line 26 def head @head end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
26 27 28 |
# File 'lib/DobLinkedList.rb', line 26 def length @length end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
26 27 28 |
# File 'lib/DobLinkedList.rb', line 26 def tail @tail end |
Instance Method Details
#<=>(other) ⇒ Object
137 138 139 140 141 |
# File 'lib/DobLinkedList.rb', line 137 def <=>(other) return nil unless other.instance_of? DobLinkedList value <=> other.value end |
#[](index) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/DobLinkedList.rb', line 172 def [](index) case index when 0, -@length @head when length-1, -1 @tail else find_node(index) end end |
#each ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/DobLinkedList.rb', line 153 def each return nil unless block_given? current = @head while current yield current current = current.next end end |
#find_first(&predicate) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/DobLinkedList.rb', line 143 def find_first(&predicate) return nil unless block_given? current = @head while current return current if predicate.call(current) current = current.next end end |
#find_node(index) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/DobLinkedList.rb', line 162 def find_node (index) current = @head count = 0 while current return current if count==index count += 1 current = current.next end end |
#insertHead(value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/DobLinkedList.rb', line 47 def insertHead(value) node = Node.new(value, nil, nil) if @head node.next = @head @head.prev = node @head = node else @head = node end @length += 1 end |
#insertMany(valuesArray) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/DobLinkedList.rb', line 75 def insertMany(valuesArray) valuesArray.each {| value | node = Node.new(value, nil, nil) unless @head @head = node else node.prev = @tail @tail.next = node end @tail = node @length += 1 } end |
#insertTail(value) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/DobLinkedList.rb', line 61 def insertTail(value) node = Node.new(value, nil, nil) unless @head @head = node else node.prev = @tail @tail.next = node end @tail = node @length += 1 end |
#remove(node) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/DobLinkedList.rb', line 119 def remove(node) return nil unless node if node == @head if @head.next.nil? @head = @tail = nil else @head = @head.next end else pr = node.prev ne = node.next pr&.next = ne ne&.prev = pr end @length -= 1 end |
#remove_head ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/DobLinkedList.rb', line 93 def remove_head if @head.next.nil? @head = @tail = nil else @head = @head.next @head.prev = nil end @length -= 1 end |
#remove_tail ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/DobLinkedList.rb', line 105 def remove_tail if @tail.prev.nil? @tail = @head = nil else @tail = @tail.prev @tail.next = nil end @length -= 1 end |
#to_s ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/DobLinkedList.rb', line 34 def to_s listString = "" current = @head while current listString = listString + current.to_s current = current.next end return listString end |