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


class item(init_data)
  {
    variable next,
             previous,
             data := init_data;
  };
 
function prepend(tail, new_data)
  {
    immutable result := item(new_data);
    result.next := tail;
    result.previous := null;
    if (tail != null)
        tail.previous := result;;
    return result;
  };
 
variable my_list := null;
my_list := prepend(my_list, "R");
my_list := prepend(my_list, "o");
my_list := prepend(my_list, "s");
my_list := prepend(my_list, "e");
my_list := prepend(my_list, "t");
my_list := prepend(my_list, "t");
my_list := prepend(my_list, "a");
 
"Items in the list going forward:"!
variable follow := my_list;
while (true)
  {
    follow.data!
    if (follow.next == null)
        break;;
  }
step
    follow := follow.next;;
 
"Items in the list going backward:"!
while (follow != null)
    follow.data!
step
    follow := follow.previous;;