September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,3 +1,5 @@
import std.stdio;
class LinkedList(T)
{
Node!(T) head, tail;
@ -65,13 +67,13 @@ class Node(T)
/** Insert an element after this one. */
void insertAfter (T value)
{
new Node!(T)(this, next, value, parent);
new Node!(T)(next, this, value, parent);
}
/** Insert an element before this one. */
void insertBefore (T value)
{
new Node!(T)(previous, this, value, parent);
new Node!(T)(this, previous, value, parent);
}
/** Remove the current node from the list. */
@ -90,8 +92,8 @@ class Node(T)
void main ()
{
char[][] sample = ["was", "it", "a", "cat", "I", "saw"];
auto list = LinkedList!(char[]).fromArray (sample);
string[] sample = ["was", "it", "a", "cat", "I", "saw"];
auto list = LinkedList!string.fromArray (sample);
for (auto elem = list.head; elem; elem = elem.next)
{
writef ("%s ", elem.value);

View file

@ -0,0 +1,66 @@
// version 1.1.2
class LinkedList<E> {
class Node<E>(var data: E, var prev: Node<E>? = null, var next: Node<E>? = null) {
override fun toString(): String {
val sb = StringBuilder(this.data.toString())
var node = this.next
while (node != null) {
sb.append(" -> ", node.data.toString())
node = node.next
}
return sb.toString()
}
}
var first: Node<E>? = null
var last: Node<E>? = null
fun addFirst(value: E) {
if (first == null) {
first = Node(value)
last = first
}
else {
val node = first!!
first = Node(value, null, node)
node.prev = first
}
}
fun addLast(value: E) {
if (last == null) {
last = Node(value)
first = last
}
else {
val node = last!!
last = Node(value, node, null)
node.next = last
}
}
fun insert(after: Node<E>?, value: E) {
if (after == null)
addFirst(value)
else if (after == last)
addLast(value)
else {
val next = after.next
val new = Node(value, after, next)
after.next = new
if (next != null) next.prev = new
}
}
override fun toString() = first.toString()
}
fun main(args: Array<String>) {
val ll = LinkedList<Int>()
ll.addFirst(1)
ll.addLast(4)
ll.insert(ll.first, 2)
ll.insert(ll.last!!.prev, 3)
println(ll)
}

View file

@ -0,0 +1,12 @@
IMPORT Basic;
TYPE
Node* = POINTER TO NodeDesc;
NodeDesc* = (* ABSTRACT *) RECORD
prev-,next-: Node;
END;
DLList* = POINTER TO DLListDesc;
DLListDesc* = RECORD
first-,last-: Node;
size-: INTEGER;
END;

View file

@ -0,0 +1,17 @@
use Collection;
class Program {
function : Main(args : String[]) ~ Nil {
list := List->New();
list->AddFront("first");
list->AddBack("last");
list->Insert("middle");
list->Forward();
do {
list->Get()->As(String)->PrintLine();
list->Previous();
}
while(list->Get() <> Nil);
}
}

View file

@ -0,0 +1,26 @@
class Node{
fcn init(_value,_prev=Void,_next=Void)
{ var value=_value, prev=_prev, next=_next; }
fcn toString{ value.toString() }
fcn append(value){ // loops not allowed: create a new Node
b,c := Node(value,self,next),next;
next=b;
if(c) c.prev=b;
b
}
fcn delete{
if(prev) prev.next=next;
if(next) next.prev=prev;
self
}
fcn last { n,p := self,self; while(n){ p,n = n,n.next } p }
fcn first { n,p := self,self; while(n){ p,n = n,n.prev } p }
fcn walker(forward=True){
dir:=forward and "next" or "prev";
Walker(fcn(rn,dir){
if(not (n:=rn.value)) return(Void.Stop);
rn.set(n.setVar(dir));
n.value;
}.fp(Ref(self),dir))
}
}

View file

@ -0,0 +1,6 @@
a:=Node("a");
a.append("c").append("d");
a.last().append("e");
a.last().first().append("b");
foreach n in (a){ print(n," ") } println();
foreach n in (a.last().walker(False)){ print(n," ") } println();