How to resolve the algorithm Singly-linked list/Traversal step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Singly-linked list/Traversal step by step in the jq programming language

Table of Contents

Problem Statement

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Singly-linked list/Traversal step by step in the jq programming language

Source code in the jq programming language

# Produce a stream of the items in the input SLL.
def items:
  while(.; .next) | .item;

def to_singly_linked_list(s):
  reduce ([s]|reverse[]) as $item (null; {$item, next:.});

# If f evaluates to empty at any item, that item is removed;
# if f evaluates to more than one item, all are added separately.
def map_singly_linked_list(f): to_singly_linked_list( items | f );

{
  "item": 1,
  "next": {
    "item": 2,
    "next": null
  }
}
| reduce items as $item (null; .+$item),
  map_singly_linked_list(- .)

  

You may also check:How to resolve the algorithm Pangram checker step by step in the Logo programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Vector step by step in the jq programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Smalltalk programming language