Sha256: 8fec36ba2629862523ac83ad0c3eb6d54cf7aa9139835ab988021eb77022aa57

Contents?: true

Size: 873 Bytes

Versions: 1

Compression:

Stored size: 873 Bytes

Contents

# frozen_string_literal: true

module Ds101
  module LinkedList
    module Common
      def find(value)
        current = head
        current = current.next while current && current.value != value
        current
      end

      def find_by(&block)
        current = head
        while current
          return current if block.call(current)

          current = current.next
        end
        nil
      end

      def each
        current = head
        while current
          yield current
          current = current.next
        end
      end

      def concat(other_list)
        other_list.each do |node|
          append(node.value)
        end
      end

      def to_a
        arr = []
        each do |node|
          arr << node.value
        end
        arr
      end

      def clear
        remove_head while length.positive?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ds_101-0.1.0 lib/ds_101/linked_list/common.rb