Class: Listas

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nutri/lista.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializenil

Initialize Initializes all to nil



17
18
19
20
21
# File 'lib/nutri/lista.rb', line 17

def initialize()
	@head = nil
	@tail = nil
	@size = 0
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



11
12
13
# File 'lib/nutri/lista.rb', line 11

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/nutri/lista.rb', line 11

def size
  @size
end

#tailObject (readonly)

Returns the value of attribute tail.



11
12
13
# File 'lib/nutri/lista.rb', line 11

def tail
  @tail
end

Instance Method Details

#eachobj.value

Each Method, necessarie for the enumeration

Returns:

  • (obj.value)
    gives back the value of the actual node


27
28
29
30
31
32
33
# File 'lib/nutri/lista.rb', line 27

def each()
	act = @head
	while act != nil
		yield act.value
		act = act.next
	end
end

#each_listasObject

Tarea 4



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/nutri/lista.rb', line 143

def each_listas
	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

#for_listasObject

Tarea 3



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/nutri/lista.rb', line 125

def for_listas
	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

#popstring

Pops the last Element of the list and prints it via the to_s method.

Returns:

  • (string)
    Returns the String of the Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nutri/lista.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

#push(obj) ⇒ string

Pushes a new Object at the end of the list.

Parameters:

  • obj (object)
    Object that should be placed at the end of the list.

Returns:

  • (string)
    Returns the String of the Object


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nutri/lista.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

#pushn(ar) ⇒ Object

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


100
101
102
103
104
# File 'lib/nutri/lista.rb', line 100

def pushn(ar)
	ar.each do |i|
           push(i)
       end
end