Class: Algorithmable::DataStructs::Bag

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/algorithmable/data_structs/bag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBag

Returns a new instance of Bag.


7
8
9
10
# File 'lib/algorithmable/data_structs/bag.rb', line 7

def initialize
  @head = nil
  @size = 0
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head


5
6
7
# File 'lib/algorithmable/data_structs/bag.rb', line 5

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size


5
6
7
# File 'lib/algorithmable/data_structs/bag.rb', line 5

def size
  @size
end

Instance Method Details

#add(item) ⇒ Object


16
17
18
19
20
21
# File 'lib/algorithmable/data_structs/bag.rb', line 16

def add(item)
  old_head = @head
  @head = Node.new(item)
  @head.succ = old_head
  @size = @size.next
end

#each(&block) ⇒ Object


23
24
25
# File 'lib/algorithmable/data_structs/bag.rb', line 23

def each(&block)
  @head.each(&block) if @head
end

#empty?Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/algorithmable/data_structs/bag.rb', line 12

def empty?
  @head.nil?
end