Class: Exam::DList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/exam/d_list.rb

Overview

create a DList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(v) ⇒ DList

Returns a new instance of DList.



19
20
21
22
23
24
25
26
27
# File 'lib/exam/d_list.rb', line 19

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

Instance Attribute Details

#headObject

Returns the value of attribute head.



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

def head
  @head
end

#tailObject

Returns the value of attribute tail.



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

def tail
  @tail
end

Instance Method Details

#each {|aux[:value]| ... } ⇒ Object

Yields:

  • (aux[:value])


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

def each
	aux=@head
	while aux[:next]
		yield aux[:value]
		aux=aux[:next]
	end
	yield aux[:value]
end

#popHeadObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/exam/d_list.rb', line 29

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

#popTailObject



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

def popTail
	nodo = @head
	if nodo
		while nodo[:next] do
    		nodo=nodo[:next]
  	end
  	aux=nodo[:prev]
  	aux[:next]=nil
  	nodo[:prev]=nil
  	return nodo[:value]
  end
end

#pushHead(v) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/exam/d_list.rb', line 53

def pushHead(v)
  if v.kind_of?(Array)
    for i in 0..v.size-1
        aux = @head
        nodo=Node.new(v[i], aux, nil)
        @head=nodo
        aux[:prev]=@head
    end
  else
    aux = @head
    nodo=Node.new(v, aux, nil)
    @head=nodo
    aux[:prev]=@head
  end
end

#pushTail(v) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/exam/d_list.rb', line 69

def pushTail(v)
   aux = @head

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

#sizeObject



89
90
91
92
93
94
95
96
97
# File 'lib/exam/d_list.rb', line 89

def size
  aux=@head
  n=0
  while aux[:next] do
    n=n+1
    aux=aux[:next]
  end
  n=n+1
end

#to_isObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/exam/d_list.rb', line 110

def to_is
  aux=@head
  s=""
  while aux[:next] do
    aux=aux[:next]
  end
  while aux[:prev] do
    s = s + "#{aux[:value]} "
    aux=aux[:prev]
  end
  s = s + "#{aux[:value]}"
  #puts "#{s}"
end

#to_sObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/exam/d_list.rb', line 99

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