How to resolve the algorithm Singly-linked list/Element insertion step by step in the Scheme programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Singly-linked list/Element insertion step by step in the Scheme programming language

Table of Contents

Problem Statement

Using this method, insert an element C into a list comprised of elements A->B, following element A.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Singly-linked list/Element insertion step by step in the Scheme programming language

Source code in the scheme programming language

(define (insert-after a b lst)
  (if (null? lst)
      lst       ; This should be an error, but we will just return the list untouched
      (let ((c (car lst))
            (cs (cdr lst)))
        (if (equal? a c)
            (cons a (cons b cs))
            (cons c (insert-after a b cs))))))


(define (insert-after! a b lst)
  (let ((pos (member a lst)))
    (if pos
        (set-cdr! pos (cons b (cdr pos))))))


  

You may also check:How to resolve the algorithm Loops/For step by step in the Lisaac programming language
You may also check:How to resolve the algorithm Bitmap/Flood fill step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Archimedean spiral step by step in the Frink programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Scala programming language
You may also check:How to resolve the algorithm Euler's identity step by step in the Haskell programming language