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


uses system ;
 
type
 
   // declare the list pointer type
   plist = ^List ;
 
   // declare the list type, a generic data pointer prev and next pointers
   List = record
      data : pointer ;
      prev : pList ;
      next : pList ;
   end;
 
// since this task is just showing the traversal I am not allocating the memory and setting up the root node etc.
// Note the use of the carat symbol for de-referencing the pointer.
 
begin   
 
   // beginning to end
   while not (pList^.Next = NIL) do pList := pList^.Next ;
 
   // end to beginning
   while not (pList^.prev = NIL) do pList := pList^.prev ;   
 
end;