Traverse from the beginning of a doubly-linked list to the end, and from the end to the beginning.



See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


--------------
-- TRAVERSAL:
--------------
List.iterateForward = function(self)
  local function iter(self, node)
    if node then return node.next else return self.head end
  end
  return iter, self, nil
end
List.iterateReverse = function(self)
  local function iter(self, node)
    if node then return node.prev else return self.tail end
  end
  return iter, self, nil
end
 
---------
-- TEST:
---------
local list = List()
for i = 1, 5 do list:insertTail(i) end
io.write("Forward: ") for node in list:iterateForward() do io.write(node.data..",") end print()
io.write("Reverse: ") for node in list:iterateReverse() do io.write(node.data..",") end print()