class References::List
Is a double-linked list
Public Class Methods
new(*vals)
click to toggle source
# File lib/references/list.rb, line 8 def initialize(*vals) if vals.length == 0 @head = nil @last = nil else for val in vals do pushEnd val end end end
Public Instance Methods
each() { |value| ... }
click to toggle source
# File lib/references/list.rb, line 19 def each aux = @head while aux != nil yield aux.value aux = aux.next end end
head()
click to toggle source
# File lib/references/list.rb, line 93 def head if @head != nil @head.value else nil end end
last()
click to toggle source
# File lib/references/list.rb, line 101 def last if @last != nil @last.value else nil end end
length()
click to toggle source
# File lib/references/list.rb, line 83 def length aux = @head count = 0 while aux!=nil count = count + 1 aux = aux.next end count end
pushEnd(val)
click to toggle source
# File lib/references/list.rb, line 72 def pushEnd(val) if @last==nil @head = @last = Node.new(val,nil,nil) else aux = Node.new(val,nil,@last) @last.next = aux @last = aux end nil end
put(val)
click to toggle source
# File lib/references/list.rb, line 61 def put(val) if @head == nil @head = @last = Node.new(val,nil,nil) else aux = Node.new(val,@head, nil) @head.back = aux @head = aux end nil end
takeFirst()
click to toggle source
# File lib/references/list.rb, line 27 def takeFirst if (@head==@last) if @head == nil return nil end value = @head.value @head = nil @last = nil return value else value = @head.value @head = @head.next @head.back = nil return value end end
takeLast()
click to toggle source
# File lib/references/list.rb, line 44 def takeLast if (@head==@last) if @last == nil return nil end value = @last.value @head = nil @last = nil return value else value = @last.value @last = @last.back @last.next = nil return value end end
to_s()
click to toggle source
# File lib/references/list.rb, line 109 def to_s (self.sort.map { |x| x.formatAPA }).join("\n") end