Class: Exam::List

Inherits:
Object
  • Object
show all
Defined in:
lib/exam/list.rb

Overview

create a List

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(v) ⇒ List

Returns a new instance of List.



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

def initialize(v)
  if v.kind_of?(Array)
    @head = Node.new(v[0],nil)
    v.shift
    push(v)
  else
    @head = Node.new(v, nil)
  end
end

Instance Attribute Details

#headObject

Returns the value of attribute head.



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

def head
  @head
end

Instance Method Details

#popHeadObject



19
20
21
22
23
# File 'lib/exam/list.rb', line 19

def popHead
	nodo = @head
  @head=@head[:next]
  return nodo[:value]
end

#push(v) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/exam/list.rb', line 25

def push(v)
  aux = @head
  if v.kind_of?(Array)
    for i in 0..v.size-1
      a=v[i]
      while aux[:next]!=nil do
        aux=aux[:next]
      end
      nuevo_nodo=Node.new(a, nil)
      aux[:next]=nuevo_nodo
    end
  else
    while aux[:next] do
      aux=aux[:next]
    end
    nuevo_nodo=Node.new(v, nil)
    aux[:next]=nuevo_nodo
  end
end

#to_sObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/exam/list.rb', line 45

def to_s
  aux=@head
  s="Lista:  "
  while aux[:next] do
    s = s + "#{aux[:value]}" + "  ->  "
    aux=aux[:next]
  end
  s = s + "#{aux[:value]}"
  #puts "#{s}"
end