Singly-Linked List (traversal)

From Rosetta Code

Jump to: navigation, search

Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.

Code examples should be formatted along the lines of one of the existing prototypes.

Traverse from the beginning of a singly-linked list to the end.

Contents

[edit] Ada

The Ada standard container library provides a doubly-linked list. List traversal is demonstrated for the forward links.

with Ada.Containers.Doubly_Linked_Lists;
with Ada.Text_Io; use Ada.Text_Io;

procedure Traversal_Example is
   package Int_List is new Ada.Containers.Doubly_Linked_Lists(Integer);
   use Int_List;
   procedure Print(Position : Cursor) is
   begin
      Put_Line(Integer'Image(Element(Position)));
   end Print;
   The_List : List;
begin
   for I in 1..10 loop
      The_List.Append(I);
   end loop;
   -- Traverse the list, calling Print for each value
   The_List.Iterate(Print'access);
end traversal_example;

[edit] ALGOL 68

Linked lists are not built into ALGOL 68 per se, nor any available standard library. However Linked lists are presented in standard text book examples. Or can be manually constructed, eg:

MODE STRINGLIST = STRUCT(STRING value, REF STRINGLIST next);

STRINGLIST list := ("Big",
  LOC STRINGLIST := ("fjords",
    LOC STRINGLIST := ("vex",
      LOC STRINGLIST := ("quick",
        LOC STRINGLIST := ("waltz",
          LOC STRINGLIST := ("nymph",NIL))))));

REF STRINGLIST node := list;
WHILE REF STRINGLIST(node) ISNT NIL DO
  print((value OF node, space));
  node := next OF node
OD;
print((newline))
Output:
Big fjords vex quick waltz nymph 

[edit] Java

Works with: Java version 1.5+


For Java.util.LinkedList<T>, use a for each loop (from Loop Structures):

LinkedList<Type> list = new LinkedList<Type>();

for(Type i: list){
  //each element will be in variable "i"
  System.out.println(i);
}

Note that Java.util.LinkedList can also perform as a stack, queue, or doubly-linked list.

[edit] OCaml

# let li = ["big"; "fjords"; "vex"; "quick"; "waltz"; "nymph"] in
  List.iter print_endline li ;;
big
fjords
vex
quick
waltz
nymph
- : unit = ()

[edit] Python

 
for node in lst:
    print node.value
 

Any Python class can define next() and __iter__() methods so that it can be used with the normal for iteration syntax. In this example the "lst" could be an instance of any Python list, tuple, dictionary, or any sort of object which defines an iterator. It could also be a generator (a type of function which yields results upon each successive invocation). The notion of a "singly linked list" is somewhat more primitive than normal Python built-in objects.

class LinkedList(object):
  def __init__(self, value, next):
    self.value = value;
    self.next = next
  def __iter__(self):
    node = self
    while node != None:
      yield node.value
      node = node.next;
 
lst = LinkedList("big",  next=
  LinkedList(value="fjords",next=
    LinkedList(value="vex",   next=
      LinkedList(value="quick", next=
        LinkedList(value="waltz", next=
          LinkedList(value="nymph", next=None))))));
 
for value in lst:
  print value,;
print

Output:

big fjords vex quick waltz nymph
Personal tools