class Dll
Attributes
head[R]
Getter for the head node @return [DllNode] The first node on the list or nil if there is none
tail[R]
Getter for the tail node @return [DllNode] The last node on the list or nil if there is none
Public Class Methods
new()
click to toggle source
Initializes to an empty list
# File lib/TDD/Dll.rb, line 13 def initialize @head = nil @tail = nil end
Public Instance Methods
each() { |value| ... }
click to toggle source
Iterates over the list and yields for each item. The yield passes the value of each element to the block
# File lib/TDD/Dll.rb, line 73 def each curr_node = @head while (curr_node != nil) yield curr_node.value curr_node = curr_node.next end end
extract_head()
click to toggle source
Deletes the first element on the list
# File lib/TDD/Dll.rb, line 45 def extract_head if( @head != nil ) if( @head.next == nil ) @head = nil @tail = nil else @head = @head.next @head.prev.next = nil @head.prev = nil end end end
extract_tail()
click to toggle source
Deletes the last element on the list
# File lib/TDD/Dll.rb, line 59 def extract_tail if( @tail != nil ) if( @tail.prev == nil) @tail = nil @head = nil else @tail = @tail.prev @tail.next.prev = nil @tail.next = nil end end end
insert_head(value)
click to toggle source
Insert a new value, having it be the first on the list @param value What you want to be inserted to the list
# File lib/TDD/Dll.rb, line 20 def insert_head(value) if( @head == nil ) @head = DllNode.new( nil, value, nil ) @tail = @head else aux = DllNode.new( nil, value, @head ) @head.prev = aux @head = aux end end
insert_tail(value)
click to toggle source
Insert a new value, having it be the last on the list @param value What you want to be inserted to the list
# File lib/TDD/Dll.rb, line 33 def insert_tail(value) if( @tail == nil ) @head = DllNode.new( nil, value, nil ) @tail = @head else aux = DllNode.new( @tail, value, nil ) @tail.next = aux @tail = aux end end