Class: Liste
Overview
Doubly Linked List Enumerable functions included
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Instance Method Summary collapse
-
#each ⇒ obj.value
Each Method, necessarie for the enumeration.
-
#eachs ⇒ Object
Tarea 4 - each.
-
#fors ⇒ Object
Tarea 3 - for.
-
#initialize ⇒ nil
constructor
Initialize Initializes all to nil.
-
#pop ⇒ string
Pops the last Element of the list and prints it via the to_s method.
- #popb ⇒ Object
- #popbn(i) ⇒ Object
- #popn(i) ⇒ Object
-
#push(obj) ⇒ string
Pushes a new Object at the end of the list.
- #pushb(obj) ⇒ Object
- #pushbn(ar) ⇒ Object
- #pushn(ar) ⇒ Object
Constructor Details
#initialize ⇒ nil
Initialize Initializes all to nil
17 18 19 20 21 |
# File 'lib/prct06/list.rb', line 17 def initialize() @head = nil @tail = nil @size = 0 end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
11 12 13 |
# File 'lib/prct06/list.rb', line 11 def head @head end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
11 12 13 |
# File 'lib/prct06/list.rb', line 11 def size @size end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
11 12 13 |
# File 'lib/prct06/list.rb', line 11 def tail @tail end |
Instance Method Details
#each ⇒ obj.value
Each Method, necessarie for the enumeration
27 28 29 30 31 32 33 |
# File 'lib/prct06/list.rb', line 27 def each() act = @head while act != nil yield act.value act = act.next end end |
#eachs ⇒ Object
Tarea 4 - each
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/prct06/list.rb', line 140 def eachs sorted = [@head.value] self.each_with_index do |x, pos_x| if (pos_x != 0) sorted.each_with_index do |y, pos_y| if (pos_y == sorted.size - 1) if (x < y) sorted.insert(pos_y, x) break else sorted.push(x) break end elsif (x < y) sorted.insert(pos_y, x) break end end end end return sorted end |
#fors ⇒ Object
Tarea 3 - for
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/prct06/list.rb', line 122 def fors sorted = [@head.value] act = @head for i in (1...@size) act = act.next for j in (0..sorted.size) if (j == sorted.size) sorted.push(act.value) elsif (act.value < sorted[j]) sorted.insert(j, act.value) break end end end return sorted end |
#pop ⇒ string
Pops the last Element of the list and prints it via the to_s method.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/prct06/list.rb', line 68 def pop() if size > 0 a = @tail.value @size -= 1 @tail = @tail.prev if size > 0 @tail.next = nil else @head = nil end else puts "No elements" end return a.to_s end |
#popb ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/prct06/list.rb', line 84 def popb() if size > 0 a = @head.value @size -= 1 @head = @head.next if size > 0 @head.prev = nil else @head = nil end else puts "No elements" end return a.to_s end |
#popbn(i) ⇒ Object
115 116 117 118 119 |
# File 'lib/prct06/list.rb', line 115 def popbn(i) while @size > 0 && i > 0 do popb() end end |
#popn(i) ⇒ Object
110 111 112 113 114 |
# File 'lib/prct06/list.rb', line 110 def popn(i) while @size > 0 && i > 0 do pop() end end |
#push(obj) ⇒ string
Pushes a new Object at the end of the list.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/prct06/list.rb', line 40 def push(obj) a = Node.new(obj, nil, @tail) if size > 0 @tail.next = a else @head = a end @tail = a @size += 1 return a.value.to_s end |
#pushb(obj) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/prct06/list.rb', line 52 def pushb(obj) a = Node.new(obj, @head, nil) if size > 0 @head.prev = a else @tail = a end @head = a @size += 1 return a.value.to_s end |
#pushbn(ar) ⇒ Object
105 106 107 108 109 |
# File 'lib/prct06/list.rb', line 105 def pushbn(ar) ar.each do |i| pushb(i) end end |
#pushn(ar) ⇒ Object
100 101 102 103 104 |
# File 'lib/prct06/list.rb', line 100 def pushn(ar) ar.each do |i| push(i) end end |