Class: DS::Ring
- Inherits:
-
CyclicList
- Object
- List
- CyclicList
- DS::Ring
- Defined in:
- lib/ds/lists/ring.rb
Overview
Ring - represent list which head is linked with tail.
Instance Attribute Summary
Attributes inherited from List
Class Method Summary collapse
-
.from_array(arr) ⇒ Object
Creates ring from array.
Instance Method Summary collapse
-
#eliminate_by(k) ⇒ Object
Removes ring elements by k until there is only one element in list.
- #length ⇒ Object
Methods inherited from CyclicList
Methods inherited from List
#append, #each, #each_with_index, #empty?, #first, #initialize, #insert_after, #insert_before, #joint, #last, #looped?, #merge, #orderize, #prepend, #print, #remove, #remove!, #reverse!, #shift, #to_a, #zip?
Constructor Details
This class inherits a constructor from DS::List
Class Method Details
Instance Method Details
#eliminate_by(k) ⇒ Object
Removes ring elements by k until there is only one element in list.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ds/lists/ring.rb', line 20 def eliminate_by(k) elem = self.head prev = elem k = k-1 while prev != elem.next k.times do prev = elem elem = elem.next end prev.next = elem.next elem = prev.next end return prev.data end |
#length ⇒ Object
15 16 17 |
# File 'lib/ds/lists/ring.rb', line 15 def length cycle_size end |