class Lista
Attributes
head[RW]
tail[RW]
Public Class Methods
new()
click to toggle source
Constructor @param val as first element
# File lib/pract/list.rb, line 9 def initialize @head = @tail = nil end
Public Instance Methods
add_first(val)
click to toggle source
Add val as first element
# File lib/pract/list.rb, line 39 def add_first (val) if (@head!= nil) point = @head point.prev = Nodo.new(nil, val, point) @head = point.prev else @head = @tail = Nodo.new(nil, val, nil) end end
add_last(val)
click to toggle source
Add val as last element
# File lib/pract/list.rb, line 29 def add_last (val) if (@head!= nil) point = @tail point.next = Nodo.new(point, val, nil) @tail = point.next else @head = @tail = Nodo.new(nil, val, nil) end end
each() { |val| ... }
click to toggle source
Make list enumerable
# File lib/pract/list.rb, line 114 def each point = @head while(point != @tail) do yield point.val point = point.next end yield(point.val) end
remove(val)
click to toggle source
Remove an element @param val [value] as the value we want to remove
# File lib/pract/list.rb, line 60 def remove (val) if (@head != nil) if(@head == @tail) && (@head.val == val) @head = nil @tail = nil elsif (@head.val == val) remove_first elsif (@tail.val == val) remove_last else point = @head while (point.next != nil) && (point.val != val) point = point.next end if point != @tail before = point.prev after = point.next before.next = after after.prev = before point.prev = nil point.next = nil end end end end
remove_first()
click to toggle source
Remove the first element
# File lib/pract/list.rb, line 49 def remove_first point = @head @head = point.next end
remove_last()
click to toggle source
Remove the last element
# File lib/pract/list.rb, line 54 def remove_last point = @tail @tail = point.prev end
search(val)
click to toggle source
@return position of val[from 1] or nil is not found
# File lib/pract/list.rb, line 97 def search(val) num = 1 point = @head while(point != @tail) if(point.val == val) return num else num = num+1 point = point.next end end if @tail.val == val return num else return 0 end end
sz()
click to toggle source
@return size of element
# File lib/pract/list.rb, line 86 def sz size_list = 0 point = @head while(point != tail) size_list = size_list + 1 point = point.next end size_list = size_list+1 return size_list end
to_s()
click to toggle source
@param point as auxiliar pointer @param elements as showing variable @return string format
# File lib/pract/list.rb, line 15 def to_s elements = "[" point = @head while point != @tail elements = elements + point.val.to_s elements = elements + "], [" point = point.next end if (@head != nil) elements = elements + point.val.to_s end elements = elements + "]" end