SCOOP/Linked List

From Esolang
Jump to navigation Jump to search

Linked list

A SCOOP implementation of a doubly linked list type with a nested node type. Methods include

  • ⓐ: append a value to the end of the list
  • ⓗ: push a value on the head of the list
  • ⓟ: pop a value from the head of the list
  • ↔: return the list length
  • ⥁: iterate over the list, passing each node’s value to a callback one at a time
  • ⧟: iterate over the list, passing each node object to a callback one at a time
  • ⓕ: find and return a node with a value equal to the parameter value (uses the = method to compare)
  • Ⓕ: find and return a node with an object identical to the parameter value (uses the ≡ method to compare)
  • ⓝ: returns a new node with all data members initialized to ␣ (used internally)
  • ⓓ: delete a node (must be in the list)
  • ⓘ: insert the parameter value (as a new node) before the first value in the list that is not less than the parameter value (uses the < method to compare)

List members:

  • h: list head (first node)
  • t: list tail (last node)
  • s: list length (integer)

Node members and methods:

  • p: previous node
  • n: next node
  • v: value
  • P: set p
  • N: set n
  • V: set v

A method to create a new list:

:☆ ←h␣ ←t␣ :s0# :11#
  :↔⏎sƒ
  :ⓝ←p␣ ←n␣ ←v␣
    :P←p⒳ƒ :N←n⒳ƒ :V←v⒳ƒ
    :●pPnNvV□ ⏎●ƒ
  :ⓓ←d⒳ ←p.dp ←n.dn
    :⊨←t␣✆
    :⊭▷nP␣✆
    :H⊨⊭□
    :⊨▷pN␣ ←tp✆
    :⊭▷pNn ▷nPp✆
    :M⊨⊭□
    :⊨←b▷n≡␣ ▷b⌥H ←hn✆
    :⊭←b▷d≡t ▷b⌥M✆
    :Q⊨⊭□
    ←b▷d≡h ▷b⌥Q ←s▷s-1ƒ
  :ⓐ←●▷$ⓝ␣ ▷●Pt ▷●V⒳
    :⊨←h●✆
    :⊭▷tN●✆
    :M⊨⊭□
    ←b▷t≡␣ ▷b⌥M ←t● ←s▷s+1ƒ
  :ⓗ←●▷$ⓝ␣ ▷●Nh ▷●V⒳
    :⊨←t●✆
    :⊭▷hP●✆
    :M⊨⊭□
    ←b▷h≡␣ ▷b⌥M ←h● ←s▷s+1ƒ
  :ⓟ←r.hv ▷$ⓓh ⏎rƒ
  :⥁←c⒳ ←nh :i-1#
    :⊨:r1#✆
    :⊭←r␣ ←v.nv ▷$cv ←n.nn✆
    :M⊨⊭□
    :L←b▷n≡␣ ▷b⌥M ⏎r✆
    ▷i⥁Lƒ
  :⧟←c⒳ ←nh :i-1#
    :⊨:r1#✆
    :⊭←r␣ ▷$cn ←n.nn✆
    :M⊨⊭□
    :L←b▷n≡␣ ▷b⌥M ⏎r✆
    ▷i⥁Lƒ
  :ⓕ←v⒳ ←nh :i-1#
    :⊨:r1#✆
    :⊭←r␣ ←n.nn✆
    :Q⊨⊭□
    :⊭←w.nv ←b▷w=v ▷b⌥Q✆
    :M⊨⊭□
    :L←b▷n≡␣ ▷b⌥M ⏎r✆
    ▷i⥁L ⏎nƒ
  :Ⓕ←v⒳ ←nh :i-1#
    :⊨:r1#✆
    :⊭←r␣ ←n.nn✆
    :Q⊨⊭□
    :⊭←w.nv ←b▷w≡v ▷b⌥Q✆
    :M⊨⊭□
    :L←b▷n≡␣ ▷b⌥M ⏎r✆
    ▷i⥁L ⏎nƒ
  :ⓘ←v⒳ ←nh :i-1#
    :⊨▷$ⓗv✆
    :⊭←●▷$ⓝ␣ ▷●Vv ←x.np ▷●Px
      ▷xN● ▷●Nn ▷nP● ←s▷s+1✆
    :K⊨⊭□
    :⊭:r1# ←b▷n≡h ▷b⌥K✆
    :⊨←r␣ ←n.nn✆
    :Q⊨⊭□
    :⊨:r1# ▷$ⓐv✆
    :⊭←w.nv ←b▷w<v ▷b⌥Q✆
    :M⊨⊭□
    :L←b▷n≡␣ ▷b⌥M ⏎r✆
    ▷i⥁Lƒ
  :lhts1↔ⓝⓓⓐⓗⓟ⥁⧟ⓕⒻⓘ□
  ⏎l
ƒ

Example usage

Counting input values

This program reads integers from input and uses a linked list of counter objects to count each value in the input. It finishes by printing a list of the values and how many times each occurred.

Method C is a constructor for counter objects and takes one parameter: the number to be counted. The counter objects have the following members and methods:

  • constants 0 and 1
  • n: the number being counted
  • c: the count
  • +: increment count
  • r: reset count to zero
  • =: returns 1 if it’s parameter is equal to n, else returns 0
  • <: returns 1 if it’s parameter is less than n, else returns 0

This program should be appended to the linked list code above.

←L▷@☆␣
:C←n⒳ :11# :00# :c0#
  :+←c▷c+1ƒ :r←c0ƒ
  :=←b▷⒳=n⏎bƒ :<←b▷⒳<n⏎bƒ
  :o10nc+r=<□
  ⏎oƒ

:i-1#
:⊨←k▷@Cv ▷k+␣ ▷Lⓘk✆
:⊭←k.dv ▷k+␣✆
:Q⊨⊭□
:⊨:r1#✆
:⊭←r␣ ←d▷Lⓕv ←b▷d≡␣ ▷b⌥Q✆
:M⊨⊭□
:f←v▷1ⓘ␣ ←b▷v≡␣ ▷b⌥M ⏎r✆
▷i⥁f

:mValue`: Count" :␊
"
:s`: "
▷mⓟ␣ ▷␊ⓟ␣
:O←n.⒳n ←c.⒳c ▷nⓟ␣ ▷sⓟ␣
  ▷cⓟ␣ ▷␊ⓟ␣✆
▷L⥁O