Class: Exam::DList
Overview
create a DList
Instance Attribute Summary collapse
-
#head ⇒ Object
Returns the value of attribute head.
-
#tail ⇒ Object
Returns the value of attribute tail.
Instance Method Summary collapse
- #each {|aux[:value]| ... } ⇒ Object
-
#initialize(v) ⇒ DList
constructor
A new instance of DList.
- #popHead ⇒ Object
- #popTail ⇒ Object
- #pushHead(v) ⇒ Object
- #pushTail(v) ⇒ Object
- #size ⇒ Object
- #to_is ⇒ Object
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head.
7 8 9 |
# File 'lib/exam/d_list.rb', line 7 def head @head end |
#tail ⇒ Object
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
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 |
#popHead ⇒ Object
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 |
#popTail ⇒ Object
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 |
#size ⇒ Object
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_is ⇒ Object
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_s ⇒ Object
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 |