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,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);