Method: List#tail
- Defined in:
-
lib/nutrientes/list.rb,
lib/nutrientes/list.rb
Returns The last element of the list.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/nutrientes/list.rb', line 31 class List attr_reader :head, :tail, :size include Enumerable # Initialization of the list def initialize() @size = 0 @head = nil @tail = nil end # Defines the each method to make use of the Enumerable module def each actual = @head while actual != nil do yield actual actual = actual.nodo[:next_] end end # Inserts a node at the beginning of the list # @param [Object] nodo node to insert # @return nil def push(nodo) if @size == 0 then @head = nodo @tail = nodo else @tail.nodo[:next_] = nodo nodo.nodo[:prev_] = @tail @tail = nodo end @size += 1 end # Inserts a node at the end of the list # @param nodo [Object] node to insert # @return nil def insert(nodo) if @size == 0 then @head = nodo @tail = nodo else @head.nodo[:prev_] = nodo nodo.nodo[:next_] = @head @head = nodo end @size += 1 end # Deletes a node from the beginning of the list # @return nil def truncate if size > 1 @head = @head.nodo[:next_] @head.nodo[:prev_] = nil @size -= 1 elsif size == 1 @head = nil @tail = nil @size -= 1 end end # Deletes a node from the end of the list # @return nil def pop if size > 1 @tail = @tail.nodo[:prev_] @tail.nodo[:next_] = nil @size -= 1 elsif size == 1 @tail = nil @head = nil @size -= 1 end end end |