Class: Algorithmable::DataStructs::Bag
- Inherits:
-
Object
- Object
- Algorithmable::DataStructs::Bag
- Includes:
- Enumerable
- Defined in:
- lib/algorithmable/data_structs/bag.rb
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #add(item) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Bag
constructor
A new instance of Bag.
Constructor Details
#initialize ⇒ Bag
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
#head ⇒ Object (readonly)
Returns the value of attribute head.
5 6 7 |
# File 'lib/algorithmable/data_structs/bag.rb', line 5 def head @head end |
#size ⇒ Object (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
12 13 14 |
# File 'lib/algorithmable/data_structs/bag.rb', line 12 def empty? @head.nil? end |