|
テーマ:プログラムの話(235)
カテゴリ:Ruby
円環状のリストを書いてみた。が、使い道が思い浮かばない。 # Coding : Windows-31J class RingList attr_accessor :value def initialize @prev = self @next = self self end def add val newN = RingList.new newN.value = val @next.prev newN newN.next @next @next = newN newN.prev self newN end def next nnode @next = nnode end protected :next def prev pnode @prev = pnode end protected :prev def remove @prev.next @next @next.prev @prev nodes = @next @prev = self @next = self nodes end def prev_node @prev end def next_node @next end def length len = 0 current = self begin len += 1 current = current.next_node end until (self.equal? current ) len end def to_array arr = [] current = self begin arr << current.value current = current.next_node end until (self.equal? current ) arr end def ascending current = self begin yield current.value current = current.next_node end until (self.equal? current ) end def descending current = self begin yield current.value current = current.prev_node end until (self.equal? current ) end end if __FILE__ == $0 berryz = RingList.new berryz.value = 'キャプ' berryz = berryz.add( 'もも' ).add( 'みや' ).add( 'りしゃこ' ).add( 'ちな' ).add( 'まーさ' ).add('从 ’w’) ') p berryz.to_array p berryz.length berryz.descending do |value| p value end berryz = berryz.remove p berryz.to_array 0.upto 60 do |n| p berryz.value berryz = berryz.next_node end end お気に入りの記事を「いいね!」で応援しよう
最終更新日
2010/02/11 12:11:15 AM
コメント(0) | コメントを書く
[Ruby] カテゴリの最新記事
|