June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,11 @@
class Link
{
type<IntNumber> prop Item :: _item.
type<Link> prop Next :: _next.
constructor new(IntNumber item, Link next)
[
_item := item.
_next := next.
]
}

View file

@ -0,0 +1,59 @@
abstract type AbstractNode{T} end
struct EmptyNode{T} <: AbstractNode{T} end
mutable struct Node{T} <: AbstractNode{T}
data::T
next::AbstractNode{T}
end
Node{T}(x) where T = Node{T}(x::T, EmptyNode{T}())
mutable struct LinkedList{T}
head::AbstractNode{T}
end
LinkedList{T}() where T = LinkedList{T}(EmptyNode{T}())
LinkedList() = LinkedList{Any}()
Base.isempty(ll::LinkedList) = ll.head isa EmptyNode
function lastnode(ll::LinkedList)
if isempty(ll) throw(BoundsError()) end
nd = ll.head
while !(nd.next isa EmptyNode)
nd = nd.next
end
return nd
end
function Base.push!(ll::LinkedList{T}, x::T) where T
nd = Node{T}(x)
if isempty(ll)
ll.head = nd
else
tail = lastnode(ll)
tail.next = nd
end
return ll
end
function Base.pop!(ll::LinkedList{T}) where T
if isempty(ll)
throw(ArgumentError("list must be non-empty"))
elseif ll.head.next isa EmptyNode
nd = ll.head
ll.head = EmptyNode{T}()
else
nx = ll.head
while !isa(nx.next.next, EmptyNode)
nx = nx.next
end
nd = nx.next
nx.next = EmptyNode{T}()
end
return nd.data
end
lst = LinkedList{Int}()
push!(lst, 1)
push!(lst, 2)
push!(lst, 3)
pop!(lst) # 3
pop!(lst) # 2
pop!(lst) # 1

View file

@ -1,4 +1,8 @@
class Node(n: Int, link: Node) {
var data = n
var next = link
sealed trait List[+A]
case class Cons[+A](head: A, tail: List[A]) extends List[A]
case object Nil extends List[Nothing]
object List {
def apply[A](as: A*): List[A] =
if (as.isEmpty) Nil else Cons(as.head, apply(as.tail: _*))
}

View file

@ -1,11 +1,3 @@
class Node {
var data: Int
var next = this
def this(n: Int, link: Node) {
this()
if (next != null){
data = n
next = link
}
}
def main(args: Array[String]): Unit = {
val words = List("Rosetta", "Code", "Scala", "Example")
}

View file

@ -0,0 +1,8 @@
struct item {
transmorphic scalar value
pointer(struct item scalar) scalar next
}
struct list {
pointer(struct item scalar) scalar head, tail
}

View file

@ -0,0 +1,2 @@
list_insert_after(a, list_get(a, 2), 40)
list_show(a);

View file

@ -0,0 +1,5 @@
a = list()
list_insert_end(a, "A")
list_insert_end(a, "B")
list_insert_after(a, list_get(a, 1), "C")
list_show(a)

View file

@ -0,0 +1,7 @@
a = list()
for (i = 1; i <= 4; i++) {
list_insert(a, i)
}
while (!list_empty(a)) {
printf("%f\n", list_pop(a))
}

View file

@ -0,0 +1,7 @@
a = list()
for (i = 1; i <= 4; i++) {
list_insert_end(a, i)
}
while (!list_empty(a)) {
printf("%f\n", list_pop(a))
}

View file

@ -0,0 +1,3 @@
real scalar list_empty(struct list scalar a) {
return(a.head == NULL)
}

View file

@ -0,0 +1,36 @@
void function list_insert(struct list scalar a, transmorphic scalar x) {
struct item scalar i
i.value = x
if (a.head == NULL) {
i.next = NULL
a.head = a.tail = &i
} else {
i.next = a.head
a.head = &i
}
}
void function list_insert_end(struct list scalar a, transmorphic scalar x) {
struct item scalar i
i.value = x
i.next = NULL
if (a.head==NULL) {
a.head = a.tail = &i
} else {
(*a.tail).next = &i
a.tail = &i
}
}
void function list_insert_after(struct list scalar a,
pointer(struct item scalar) scalar p,
transmorphic scalar x) {
struct item scalar i
i.value = x
i.next = (*p).next
(*p).next = &i
if (a.tail == p) {
a.tail = &i
}
}

View file

@ -0,0 +1,22 @@
real scalar list_length(struct list scalar a) {
real scalar n
pointer(struct item scalar) scalar p
n = 0
for (p = a.head; p != NULL; p = (*p).next) {
n++
}
return(n)
}
void list_show(struct list scalar a) {
pointer(struct item scalar) scalar p
for (p = a.head; p != NULL; p = (*p).next) {
if (eltype((*p).value) == "string") {
printf("%s\n", (*p).value);
} else {
printf("%f\n", (*p).value);
}
}
}

View file

@ -0,0 +1,12 @@
pointer(struct item scalar) scalar list_get(struct list scalar a,
real scalar n) {
pointer(struct item scalar) scalar p
real scalar i
p = a.head
for (i = 1; p != NULL & i < n; i++) {
p = (*p).next
}
return(p)
}

View file

@ -0,0 +1,13 @@
transmorphic scalar list_pop(struct list scalar a) {
transmorphic scalar x
if (a.head == NULL) {
_error("empty list")
}
x = (*a.head).value
if (a.head == a.tail) {
a.head = a.tail = NULL
} else {
a.head = (*a.head).next
}
return(x)
}

View file

@ -0,0 +1,37 @@
transmorphic scalar list_remove(struct list scalar a,
real scalar n) {
pointer(struct item scalar) scalar p, q
real scalar i
transmorphic scalar x
p = a.head
if (n == 1) {
if (p == NULL) {
_error("empty list")
}
x = (*p).value
if (p == a.tail) {
a.head = a.tail = NULL
} else {
a.head = (*p).next
}
} else {
for (i = 2; p != NULL & i < n; i++) {
p = (*p).next
}
if (p == NULL) {
_error("too few elements in list")
}
q = (*p).next
if (q == NULL) {
_error("too few elements in list")
}
x = (*q).value
if (q == a.tail) {
a.tail = p
}
(*p).next = (*q).next
}
return(x)
}

View file

@ -0,0 +1,6 @@
a = list()
list_insert(a, 10)
list_insert(a, 20)
list_insert(a, 30)
list_length(a)
list_show(a);

View file

@ -0,0 +1,6 @@
a = list()
list_insert_end(a, 10)
list_insert_end(a, 20)
list_insert_end(a, 30)
list_length(a)
list_show(a);