WeixiangYan/CodeTransOcean huggingface dataset:
Task: Department numbers




Go: package main

import "fmt"

func main() {
    fmt.Println("Police  Sanitation  Fire")
    fmt.Println("------  ----------  ----")
    count := 0
    for i := 2; i < 7; i += 2 {
        for j := 1; j < 8; j++ {
            if j == i { continue }
            for k := 1; k < 8; k++ {
                if k == i || k == j { continue }
                if i + j + k != 12 { continue }
                fmt.Printf("  %d         %d         %d\n", i, j, k)
                count++
            }
        }
    }
    fmt.Printf("\n%d valid combinations\n", count)
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. DEPARTMENT-NUMBERS.

       DATA DIVISION.
       WORKING-STORAGE SECTION.       
       01 BANNER        PIC X(24) VALUE "POLICE  SANITATION  FIRE".
       01 COMBINATION.
          03 FILLER     PIC X(5)  VALUE SPACES.
          03 POLICE     PIC 9.
          03 FILLER     PIC X(11) VALUE SPACES.
          03 SANITATION PIC 9.
          03 FILLER     PIC X(5)  VALUE SPACES.
          03 FIRE       PIC 9.
       01 TOTAL         PIC 99.

       PROCEDURE DIVISION.
       BEGIN. 
           DISPLAY BANNER.
           PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2
           UNTIL POLICE IS GREATER THAN 6.
           STOP RUN.
 
       POLICE-LOOP. 
           PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1
           UNTIL SANITATION IS GREATER THAN 7.
  
       SANITATION-LOOP.
           PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1
           UNTIL FIRE IS GREATER THAN 7.
 
       FIRE-LOOP.
           ADD POLICE, SANITATION, FIRE GIVING TOTAL.
           IF POLICE IS NOT EQUAL TO SANITATION
               AND POLICE IS NOT EQUAL TO FIRE
               AND SANITATION IS NOT EQUAL TO FIRE
               AND TOTAL IS EQUAL TO 12,
               DISPLAY COMBINATION.
Task: Department numbers






PHP: <?php

$valid = 0;
for ($police = 2 ; $police <= 6 ; $police += 2) {
    for ($sanitation = 1 ; $sanitation <= 7 ; $sanitation++) {
        $fire = 12 - $police - $sanitation;
        if ((1 <= $fire) and ($fire <= 7) and ($police != $sanitation) and ($sanitation != $fire)) {
            echo 'Police: ', $police, ', Sanitation: ', $sanitation, ', Fire: ', $fire, PHP_EOL;
            $valid++;
        }
    }
}
echo $valid, ' valid combinations found.', PHP_EOL;
Task: Department numbers






PHP: <?php

$valid = 0;
for ($police = 2 ; $police <= 6 ; $police += 2) {
    for ($sanitation = 1 ; $sanitation <= 7 ; $sanitation++) {
        $fire = 12 - $police - $sanitation;
        if ((1 <= $fire) and ($fire <= 7) and ($police != $sanitation) and ($sanitation != $fire)) {
            echo 'Police: ', $police, ', Sanitation: ', $sanitation, ', Fire: ', $fire, PHP_EOL;
            $valid++;
        }
    }
}
echo $valid, ' valid combinations found.', PHP_EOL;






Ada: with Ada.Text_IO;

procedure Department_Numbers is
   use Ada.Text_IO;
begin
   Put_Line (" P S F");
   for Police in 2 .. 6 loop
      for Sanitation in 1 .. 7 loop
         for Fire in 1 .. 7 loop
            if
              Police mod 2 = 0                and
              Police + Sanitation + Fire = 12 and
              Sanitation /= Police            and
              Sanitation /= Fire              and
              Police     /= Fire
            then
               Put_Line (Police'Image & Sanitation'Image & Fire'Image);
            end if;
         end loop;
      end loop;
   end loop;
end Department_Numbers;
Task: Department numbers






PHP: <?php

$valid = 0;
for ($police = 2 ; $police <= 6 ; $police += 2) {
    for ($sanitation = 1 ; $sanitation <= 7 ; $sanitation++) {
        $fire = 12 - $police - $sanitation;
        if ((1 <= $fire) and ($fire <= 7) and ($police != $sanitation) and ($sanitation != $fire)) {
            echo 'Police: ', $police, ', Sanitation: ', $sanitation, ', Fire: ', $fire, PHP_EOL;
            $valid++;
        }
    }
}
echo $valid, ' valid combinations found.', PHP_EOL;




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. DEPARTMENT-NUMBERS.

       DATA DIVISION.
       WORKING-STORAGE SECTION.       
       01 BANNER        PIC X(24) VALUE "POLICE  SANITATION  FIRE".
       01 COMBINATION.
          03 FILLER     PIC X(5)  VALUE SPACES.
          03 POLICE     PIC 9.
          03 FILLER     PIC X(11) VALUE SPACES.
          03 SANITATION PIC 9.
          03 FILLER     PIC X(5)  VALUE SPACES.
          03 FIRE       PIC 9.
       01 TOTAL         PIC 99.

       PROCEDURE DIVISION.
       BEGIN. 
           DISPLAY BANNER.
           PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2
           UNTIL POLICE IS GREATER THAN 6.
           STOP RUN.
 
       POLICE-LOOP. 
           PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1
           UNTIL SANITATION IS GREATER THAN 7.
  
       SANITATION-LOOP.
           PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1
           UNTIL FIRE IS GREATER THAN 7.
 
       FIRE-LOOP.
           ADD POLICE, SANITATION, FIRE GIVING TOTAL.
           IF POLICE IS NOT EQUAL TO SANITATION
               AND POLICE IS NOT EQUAL TO FIRE
               AND SANITATION IS NOT EQUAL TO FIRE
               AND TOTAL IS EQUAL TO 12,
               DISPLAY COMBINATION.
Task: Fusc sequence







Python: from collections import deque
from itertools import islice, count


def fusc():
    q = deque([1])
    yield 0
    yield 1

    while True:
        x = q.popleft()
        q.append(x)
        yield x

        x += q[0]
        q.append(x)
        yield x


def longest_fusc():
    sofar = 0
    for i, f in zip(count(), fusc()):
        if f >= sofar:
            yield(i, f)
            sofar = 10 * sofar or 10


print('First 61:')
print(list(islice(fusc(), 61)))

print('\nLength records:')
for i, f in islice(longest_fusc(), 6):
    print(f'fusc({i}) = {f}')





Ada: with Ada.Text_IO;
with Ada.Integer_Text_IO;

procedure Show_Fusc is

   generic
      Precalculate : Natural;
   package Fusc_Sequences is
      function Fusc (N : in Natural) return Natural;
   end Fusc_Sequences;

   package body Fusc_Sequences is

      Precalculated_Fusc : array (0 .. Precalculate) of Natural;

      function Fusc_Slow (N : in Natural) return Natural is
      begin
         if N = 0 or N = 1 then
            return N;
         elsif N mod 2 = 0 then
            return Fusc_Slow (N / 2);
         else
            return Fusc_Slow ((N - 1) / 2) + Fusc_Slow ((N + 1) / 2);
         end if;
      end Fusc_Slow;

      function Fusc (N : in Natural) return Natural is
      begin
         if N <= Precalculate then
            return Precalculated_Fusc (N);
         elsif N mod 2 = 0 then
            return Fusc (N / 2);
         else
            return Fusc ((N - 1) / 2) + Fusc ((N + 1) / 2);
         end if;
      end Fusc;

   begin
      for N in Precalculated_Fusc'Range loop
         Precalculated_Fusc (N) := Fusc_Slow (N);
      end loop;
   end Fusc_Sequences;


   package Fusc_Sequence is
      new Fusc_Sequences (Precalculate => 200_000);

   function Fusc (N : in Natural) return Natural
     renames Fusc_Sequence.Fusc;


   procedure Print_Small_Fuscs is
      use Ada.Text_IO;
   begin
      Put_Line ("First 61 numbers in the fusc sequence:");
      for N in 0 .. 60 loop
         Put (Fusc (N)'Image);
         Put (" ");
      end loop;
      New_Line;
   end Print_Small_Fuscs;


   procedure Print_Large_Fuscs (High : in Natural) is
      use Ada.Text_IO;
      use Ada.Integer_Text_IO;
      subtype N_Range is Natural range Natural'First .. High;
      F       : Natural;
      Len     : Natural;
      Max_Len : Natural := 0;
      Placeholder : String := "       n      fusc(n)";
      Image_N     : String renames Placeholder (1  .. 8);
      Image_Fusc  : String renames Placeholder (10 .. Placeholder'Last);
   begin
      New_Line;
      Put_Line ("Printing all largest Fusc numbers upto " & High'Image);
      Put_Line (Placeholder);

      for N in N_Range loop
         F   := Fusc (N);
         Len := F'Image'Length;

          if Len > Max_Len then
             Max_Len := Len;
             Put (Image_N,    N);
             Put (Image_Fusc, F);
             Put (Placeholder);
             New_Line;
          end if;
       end loop;
   end Print_Large_Fuscs;

begin
   Print_Small_Fuscs;
   Print_Large_Fuscs (High => 20_000_000);
end Show_Fusc;
Task: Fusc sequence

C: #include<limits.h>
#include<stdio.h>

int fusc(int n){
        if(n==0||n==1)
                return n;
        else if(n%2==0)
                return fusc(n/2);
        else
                return fusc((n-1)/2) + fusc((n+1)/2);
}

int numLen(int n){
        int sum = 1;

        while(n>9){
                n = n/10;
                sum++;
        }

        return sum;
}

void printLargeFuscs(int limit){
        int i,f,len,maxLen = 1;

        printf("\n\nPrinting all largest Fusc numbers upto %d \nIndex-------Value",limit);

        for(i=0;i<=limit;i++){
                f = fusc(i);
                len = numLen(f);

                if(len>maxLen){
                        maxLen = len;
                        printf("\n%5d%12d",i,f);
                }
        }
}


int main()
{
        int i;

        printf("Index-------Value");
        for(i=0;i<61;i++)
                printf("\n%5d%12d",i,fusc(i));
        printLargeFuscs(INT_MAX);
        return 0;
}
Task: Fusc sequence



C#: using System;
using System.Collections.Generic;

static class program
{
    static int n = 61;
    static List<int> l = new List<int>() { 0, 1 };

    static int fusc(int n)
    {
        if (n < l.Count) return l[n];
        int f = (n & 1) == 0 ? l[n >> 1] : l[(n - 1) >> 1] + l[(n + 1) >> 1];
        l.Add(f); return f;
    }

    static void Main(string[] args)
    {
        bool lst = true; int w = -1, c = 0, t;
        string fs = "{0,11:n0}  {1,-9:n0}", res = "";
        Console.WriteLine("First {0} numbers in the fusc sequence:", n);
        for (int i = 0; i < int.MaxValue; i++)
        {
            int f = fusc(i); if (lst)
            {
                if (i < 61) Console.Write("{0} ", f);
                else
                {
                    lst = false;
                    Console.WriteLine();
                    Console.WriteLine("Points in the sequence where an item has more digits than any previous items:");
                    Console.WriteLine(fs, "Index\\", "/Value"); Console.WriteLine(res); res = "";
                }
            }
            if ((t = f.ToString().Length) > w)
            {
                w = t; res += (res == "" ? "" : "\n") + string.Format(fs, i, f);
                if (!lst) { Console.WriteLine(res); res = ""; } if (++c > 5) break;
            }
        }
        l.Clear();
    }
}
Task: Fusc sequence


C++: #include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <vector>

const int n = 61;
std::vector<int> l{ 0, 1 };

int fusc(int n) {
    if (n < l.size()) return l[n];
    int f = (n & 1) == 0 ? l[n >> 1] : l[(n - 1) >> 1] + l[(n + 1) >> 1];
    l.push_back(f);
    return f;
}

int main() {
    bool lst = true;
    int w = -1;
    int c = 0;
    int t;
    std::string res;
    std::cout << "First " << n << " numbers in the fusc sequence:\n";
    for (int i = 0; i < INT32_MAX; i++) {
        int f = fusc(i);
        if (lst) {
            if (i < 61) {
                std::cout << f << ' ';
            } else {
                lst = false;
                std::cout << "\nPoints in the sequence where an item has more digits than any previous items:\n";
                std::cout << std::setw(11) << "Index\\" << "  " << std::left << std::setw(9) << "/Value\n";
                std::cout << res << '\n';
                res = "";
            }
        }
        std::stringstream ss;
        ss << f;
        t = ss.str().length();
        ss.str("");
        ss.clear();
        if (t > w) {
            w = t;
            res += (res == "" ? "" : "\n");
            ss << std::setw(11) << i << "  " << std::left << std::setw(9) << f;
            res += ss.str();
            if (!lst) {
                std::cout << res << '\n';
                res = "";
            }
            if (++c > 5) {
                break;
            }
        }
    }
    return 0;
}
Task: Fusc sequence





Java: public class FuscSequence {

    public static void main(String[] args) {
        System.out.println("Show the first 61 fusc numbers (starting at zero) in a horizontal format");
        for ( int n = 0 ; n < 61 ; n++ ) {
            System.out.printf("%,d ", fusc[n]);
        }
        
        System.out.printf("%n%nShow the fusc number (and its index) whose length is greater than any previous fusc number length.%n");
        int start = 0;
        for (int i = 0 ; i <= 5 ; i++ ) {
            int val = i != 0 ? (int) Math.pow(10, i) : -1;
            for ( int j = start ; j < FUSC_MAX ; j++ ) {
                if ( fusc[j] > val ) {
                    System.out.printf("fusc[%,d] = %,d%n", j, fusc[j] );
                    start = j;
                    break;
                }
            }
        }
    }
    
    private static final int FUSC_MAX = 30000000;
    private static int[] fusc = new int[FUSC_MAX];

    static {
        fusc[0] = 0;
        fusc[1] = 1;
        for ( int n = 2 ; n < FUSC_MAX ; n++ ) {
            fusc[n] = (n % 2 == 0 ? fusc[n/2] : fusc[(n-1)/2] + fusc[(n+1)/2]);
        }
    }
}
Task: Fusc sequence







Python: from collections import deque
from itertools import islice, count


def fusc():
    q = deque([1])
    yield 0
    yield 1

    while True:
        x = q.popleft()
        q.append(x)
        yield x

        x += q[0]
        q.append(x)
        yield x


def longest_fusc():
    sofar = 0
    for i, f in zip(count(), fusc()):
        if f >= sofar:
            yield(i, f)
            sofar = 10 * sofar or 10


print('First 61:')
print(list(islice(fusc(), 61)))

print('\nLength records:')
for i, f in islice(longest_fusc(), 6):
    print(f'fusc({i}) = {f}')
Task: Fusc sequence








VB: Module Module1

    Dim n As Integer = 61, l As List(Of Integer) = {0, 1}.ToList

    Function fusc(n As Integer) As Integer
        If n < l.Count Then Return l(n)
        fusc = If((n And 1) = 0, l(n >> 1), l((n - 1) >> 1) + l((n + 1) >> 1))
        l.Add(fusc)
    End Function

    Sub Main(args As String())
        Dim lst As Boolean = True, w As Integer = -1, c As Integer = 0,
            fs As String = "{0,11:n0}  {1,-9:n0}", res As String = ""
        Console.WriteLine("First {0} numbers in the fusc sequence:", n)
        For i As Integer = 0 To Integer.MaxValue
            Dim f As Integer = fusc(i)
            If lst Then
                If i < 61 Then
                    Console.Write("{0} ", f)
                Else
                    lst = False
                    Console.WriteLine()
                    Console.WriteLine("Points in the sequence where an item has more digits than any previous items:")
                    Console.WriteLine(fs, "Index\", "/Value") : Console.WriteLine(res) : res = ""
                End If
            End If
            Dim t As Integer = f.ToString.Length
            If t > w Then
                w = t
                res &= If(res = "", "", vbLf) & String.Format(fs, i, f)
                If Not lst Then Console.WriteLine(res) : res = ""
                c += 1 : If c > 5 Then Exit For
            End If
        Next : l.Clear()
    End Sub
End Module
Task: Fusc sequence




Go: package main

import (
    "fmt"
    "strconv"
)

func fusc(n int) []int {
    if n <= 0 {
        return []int{}
    }
    if n == 1 {
        return []int{0}
    }    
    res := make([]int, n)
    res[0] = 0
    res[1] = 1
    for i := 2; i < n; i++ {
        if i%2 == 0 {
            res[i] = res[i/2]
        } else {
            res[i] = res[(i-1)/2] + res[(i+1)/2]
        }
    }
    return res
}

func fuscMaxLen(n int) [][2]int {
    maxLen := -1
    maxFusc := -1
    f := fusc(n)
    var res [][2]int
    for i := 0; i < n; i++ {
        if f[i] <= maxFusc {
            continue 
        }
        maxFusc = f[i]
        le := len(strconv.Itoa(f[i]))
        if le > maxLen {
            res = append(res, [2]int{i, f[i]})
            maxLen = le
        }
    }
    return res
}

func commatize(n int) string {
    s := fmt.Sprintf("%d", n)
    if n < 0 {
        s = s[1:]
    }
    le := len(s)
    for i := le - 3; i >= 1; i -= 3 {
        s = s[0:i] + "," + s[i:]
    }
    if n >= 0 {
        return s
    }
    return "-" + s
}

func main() {
    fmt.Println("The first 61 fusc numbers are:")
    fmt.Println(fusc(61))
    fmt.Println("\nThe fusc numbers whose length > any previous fusc number length are:")
    res := fuscMaxLen(20000000)  
    for i := 0; i < len(res); i++ {
        fmt.Printf("%7s (index %10s)\n", commatize(res[i][1]), commatize(res[i][0]))
    }
}
Task: Singly-linked list_Traversal



C#: var current = [head of list to traverse]
while(current != null)
{
    

    current = current.Next;
}









Ada: 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;
   
   The_List.Iterate(Print'access);
end traversal_example;
Task: Singly-linked list_Traversal

C: struct link *first;

struct link *iter;
for(iter = first; iter != NULL; iter = iter->next) {
  
}











Ada: 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;
   
   The_List.Iterate(Print'access);
end traversal_example;
Task: Singly-linked list_Traversal


C++: #include <iostream>
#include <forward_list>

int main()
{
    std::forward_list<int> list{1, 2, 3, 4, 5};
    for (int e : list)
        std::cout << e << std::endl;
}










Ada: 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;
   
   The_List.Iterate(Print'access);
end traversal_example;
Task: Singly-linked list_Traversal




Go: start := &Ele{"tacos", nil}
end := start.Append("burritos")
end = end.Append("fajitas")
end = end.Append("enchilatas")
for iter := start; iter != nil; iter = iter.Next {
    fmt.Println(iter)
}








Ada: 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;
   
   The_List.Iterate(Print'access);
end traversal_example;
Task: Singly-linked list_Traversal





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

for(Type i: list){
  
  System.out.println(i);
}







Ada: 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;
   
   The_List.Iterate(Print'access);
end traversal_example;
Task: Singly-linked list_Traversal







Python: for node in lst:
    print node.value





Ada: 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;
   
   The_List.Iterate(Print'access);
end traversal_example;
Task: Singly-linked list_Traversal








VB: Private Sub Iterate(ByVal list As LinkedList(Of Integer))
    Dim node = list.First
    Do Until node Is Nothing
        node = node.Next
    Loop
    End Sub




Ada: 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;
   
   The_List.Iterate(Print'access);
end traversal_example;
Task: Singly-linked list_Traversal

C: struct link *first;

struct link *iter;
for(iter = first; iter != NULL; iter = iter->next) {
  
}
Task: Singly-linked list_Traversal



C#: var current = [head of list to traverse]
while(current != null)
{
    

    current = current.Next;
}
Task: Singly-linked list_Traversal


C++: #include <iostream>
#include <forward_list>

int main()
{
    std::forward_list<int> list{1, 2, 3, 4, 5};
    for (int e : list)
        std::cout << e << std::endl;
}
Task: Singly-linked list_Traversal





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

for(Type i: list){
  
  System.out.println(i);
}
Task: Singly-linked list_Traversal







Python: for node in lst:
    print node.value
Task: Singly-linked list_Traversal








VB: Private Sub Iterate(ByVal list As LinkedList(Of Integer))
    Dim node = list.First
    Do Until node Is Nothing
        node = node.Next
    Loop
    End Sub
Task: Singly-linked list_Traversal




Go: start := &Ele{"tacos", nil}
end := start.Append("burritos")
end = end.Append("fajitas")
end = end.Append("enchilatas")
for iter := start; iter != nil; iter = iter.Next {
    fmt.Println(iter)
}
Task: Singly-linked list_Traversal



C#: var current = [head of list to traverse]
while(current != null)
{
    

    current = current.Next;
}








Fortran: subroutine traversal(list,proc)
   type(node), target    :: list
   type(node), pointer   :: current
   interface
      subroutine proc(node)
         real, intent(in) :: node
      end subroutine proc
   end interface
   current => list
   do while ( associated(current) )
      call proc(current%data)
      current => current%next
   end do
end subroutine traversal
Task: Singly-linked list_Traversal


C++: #include <iostream>
#include <forward_list>

int main()
{
    std::forward_list<int> list{1, 2, 3, 4, 5};
    for (int e : list)
        std::cout << e << std::endl;
}









Fortran: subroutine traversal(list,proc)
   type(node), target    :: list
   type(node), pointer   :: current
   interface
      subroutine proc(node)
         real, intent(in) :: node
      end subroutine proc
   end interface
   current => list
   do while ( associated(current) )
      call proc(current%data)
      current => current%next
   end do
end subroutine traversal
Task: Singly-linked list_Traversal

C: struct link *first;

struct link *iter;
for(iter = first; iter != NULL; iter = iter->next) {
  
}










Fortran: subroutine traversal(list,proc)
   type(node), target    :: list
   type(node), pointer   :: current
   interface
      subroutine proc(node)
         real, intent(in) :: node
      end subroutine proc
   end interface
   current => list
   do while ( associated(current) )
      call proc(current%data)
      current => current%next
   end do
end subroutine traversal