Class: Lista

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nodelist/list.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Lista) initialize

Returns a new instance of Lista



6
7
8
9
# File 'lib/nodelist/list.rb', line 6

def initialize()
  @head = nil
  @lastnext = nil
end

Instance Attribute Details

- (Object) head

Returns the value of attribute head



5
6
7
# File 'lib/nodelist/list.rb', line 5

def head
  @head
end

- (Object) lastnext

Returns the value of attribute lastnext



5
6
7
# File 'lib/nodelist/list.rb', line 5

def lastnext
  @lastnext
end

- (Object) puntero

Returns the value of attribute puntero



5
6
7
# File 'lib/nodelist/list.rb', line 5

def puntero
  @puntero
end

Instance Method Details

- (Object) add(*args)



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nodelist/list.rb', line 39

def add(*args)
  args.each do |value|
    if @head == nil then
      @head = Node.new(value, nil, nil)
      @lastnext = @head
    else
      last = Node.new(value, nil, @lastnext)
      last.father = @lastnext
      @lastnext.next = last
      @lastnext = last
    end
  end
end

- (Object) add_s(value)



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nodelist/list.rb', line 28

def add_s(value) 
    if @head == nil then
      @head = Node.new(value, nil, nil)
      @lastnext = @head
    else
      last = Node.new(value, nil, @lastnext)
      last.father = @lastnext
      @lastnext.next = last
      @lastnext = last
    end
end

- (Object) each



10
11
12
13
14
15
16
# File 'lib/nodelist/list.rb', line 10

def each
 aux = @head
 while aux != nil
     yield aux.value
     aux = aux.next
 end
end

- (Object) first



52
53
54
55
56
57
58
59
60
# File 'lib/nodelist/list.rb', line 52

def first
  if @head != nil then
    aux = @head.value
    @head = @head.next
    return aux
  else
    return -1
  end
end

- (Object) reverse



21
22
23
24
25
26
27
# File 'lib/nodelist/list.rb', line 21

def reverse
  list = Array.new
  @head.reverse{|x| list.push(x.value)}
  @head = nil
  @lastnext = nil
  list.each{|x| self.add(x)}
end

- (Object) reverse_each



17
18
19
# File 'lib/nodelist/list.rb', line 17

def reverse_each
    @head.each {yield @lastnext.value; @lastnext=@lastnext.father} if @lastnext != nil
end

- (Object) to_a



64
65
66
67
68
69
70
71
72
73
# File 'lib/nodelist/list.rb', line 64

def to_a
  a_value = Array.new
  pointer = @head
  while pointer.next != nil do
    a_value.push(pointer.value)
    pointer = pointer.next
  end
  a_value.push(pointer.value)
  a_value.to_s
end

- (Object) to_s



61
62
63
# File 'lib/nodelist/list.rb', line 61

def to_s
  "#{@head}"
end