Class: DS::CyclicList

Inherits:
List
  • Object
show all
Defined in:
lib/ds/lists/cyclic_list.rb

Overview

Class represent list with cycle.

Direct Known Subclasses

Ring

Instance Attribute Summary

Attributes inherited from List

#head, #tail

Instance Method Summary collapse

Methods inherited from List

#append, #each, #each_with_index, #empty?, #first, from_array, #initialize, #insert_after, #insert_before, #joint, #last, #length, #looped?, #merge, #orderize, #prepend, #print, #remove, #remove!, #reverse!, #shift, #to_a, #zip?

Constructor Details

This class inherits a constructor from DS::List

Instance Method Details

#cycle_sizeObject

Returns cycle size. If list has no cycles returns 0.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ds/lists/cyclic_list.rb', line 7

def cycle_size
  counter = 0
  if start = self.joint
    counter = 1
    elem = joint.next
    while elem != start
      elem = elem.next
      counter += 1
    end
  end
  counter
end