all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1 @@
Define the data structure for a [[singly-linked list]] element. Said element should contain a data member capable of holding a numeric value, and the link to the next element should be mutable.

View file

@ -0,0 +1,2 @@
---
note: Data Structures

View file

@ -0,0 +1,3 @@
(let ((elem 8)
(next (list 6 7 5 3 0 9)))
(cons elem next))

View file

@ -0,0 +1,6 @@
MODE DATA = STRUCT ( ... );
MODE LINK = STRUCT (
REF LINK next,
DATA value
);

View file

@ -0,0 +1,23 @@
BEGIN {
NIL = 0
HEAD = 1
LINK = 1
VALUE = 2
delete list
initList()
}
function initList() {
delete list
list[HEAD] = makeNode(NIL, NIL)
}
function makeNode(link, value) {
return link SUBSEP value
}
function getNode(part, nodePtr, linkAndValue) {
split(list[nodePtr], linkAndValue, SUBSEP)
return linkAndValue[part]
}

View file

@ -0,0 +1,13 @@
package
{
public class Node
{
public var data:Object = null;
public var link:Node = null;
public function Node(obj:Object)
{
data = obj;
}
}
}

View file

@ -0,0 +1,6 @@
type Link;
type Link_Access is access Link;
type Link is record
Next : Link_Access := null;
Data : Integer;
end record;

View file

@ -0,0 +1,2 @@
element = 5 ; data
element_next = element2 ; link to next element

View file

@ -0,0 +1 @@
DIM node{pNext%, iData%}

View file

@ -0,0 +1,6 @@
new$link:?link1
& new$link:?link2
& first thing:?(link1..data)
& secundus:?(link2..data)
& '$link2:(=?(link1..next))
& !(link1..next..data)

View file

@ -0,0 +1,5 @@
struct link
{
link* next;
int data;
};

View file

@ -0,0 +1,6 @@
struct link
{
link* next;
int data;
link(int a_data, link* a_next = 0): next(a_next), data(a_data) {}
};

View file

@ -0,0 +1 @@
link* small_primes = new link(2, new link(3, new link(5, new link(7))));

View file

@ -0,0 +1,6 @@
template<typename T> struct link
{
link* next;
T data;
link(T a_data, link* a_next = 0): next(a_next), data(a_data) {}
};

View file

@ -0,0 +1,4 @@
struct link {
struct link *next;
int data;
};

View file

@ -0,0 +1,3 @@
import StdMaybe
:: Link t = { next :: Maybe (Link t), data :: t }

View file

@ -0,0 +1 @@
(cons 1 (cons 2 (cons 3 nil)) => (1 2 3)

View file

@ -0,0 +1,9 @@
struct SLinkedNode(T) {
T data;
typeof(this)* next;
}
void main() {
alias SLinkedNode!int N;
N* n = new N(10);
}

View file

@ -0,0 +1,6 @@
Type
pOneWayList = ^OneWayList;
OneWayList = record
pData : pointer ;
Next : pOneWayList ;
end;

View file

@ -0,0 +1,13 @@
interface LinkedList guards LinkedListStamp {}
def empty implements LinkedListStamp {
to null() { return true }
}
def makeLink(value :int, var next :LinkedList) {
def link implements LinkedListStamp {
to null() { return false }
to value() { return value }
to next() { return next }
to setNext(new) { next := new }
}
return link
}

View file

@ -0,0 +1,4 @@
TUPLE: linked-list data next ;
: <linked-list> ( data -- linked-list )
linked-list new swap >>data ;

View file

@ -0,0 +1,11 @@
class Node
{
const Int value // keep value fixed
Node? successor // allow successor to change, also, can be 'null', for end of list
new make (Int value, Node? successor := null)
{
this.value = value
this.successor = successor
}
}

View file

@ -0,0 +1,3 @@
0 value numbers
: push ( n -- )
here swap numbers , , to numbers ;

View file

@ -0,0 +1,8 @@
: length ( list -- u )
0 swap begin dup while 1 under+ @ repeat drop ;
: head ( list -- x )
cell+ @ ;
: .numbers ( list -- )
begin dup while dup head . @ repeat drop ;

View file

@ -0,0 +1,8 @@
type node
real :: data
type( node ), pointer :: next => null()
end type node
!
!. . . .
!
type( node ) :: head

View file

@ -0,0 +1,18 @@
type Ele struct {
Data interface{}
Next *Ele
}
func (e *Ele) Append(data interface{}) *Ele {
if e.Next == nil {
e.Next = &Ele{data, nil}
} else {
tmp := &Ele{data, e.Next}
e.Next = tmp
}
return e.Next
}
func (e *Ele) String() string {
return fmt.Sprintf("Ele: %v", e.Data)
}

View file

@ -0,0 +1,5 @@
class ListNode {
Object payload
ListNode next
String toString() { "${payload} -> ${next}" }
}

View file

@ -0,0 +1,4 @@
def n1 = new ListNode(payload:25)
n1.next = new ListNode(payload:88)
println n1

View file

@ -0,0 +1 @@
data List a = Nil | Cons a (List a)

View file

@ -0,0 +1 @@
data IntList s = Nil | Cons Integer (STRef s (IntList s))

View file

@ -0,0 +1 @@
record Node (value, successor)

View file

@ -0,0 +1,5 @@
class Node (value, successor)
initially (value, successor)
self.value := value
self.successor := successor
end

View file

@ -0,0 +1,5 @@
procedure main ()
n := Node(1, Node (2))
write (n.value)
write (n.successor.value)
end

View file

@ -0,0 +1,2 @@
list=: 0 2$0
list

View file

@ -0,0 +1,3 @@
list=: ,: _ 42
list
_ 42

View file

@ -0,0 +1,7 @@
list=: 0 2$a: NB. creates list with 0 items
list
list=: ,: (<_) , <'some text' NB. creates list with 1 item
list
+-+---------+
|_|some text|
+-+---------+

View file

@ -0,0 +1,5 @@
class Link
{
Link next;
int data;
}

View file

@ -0,0 +1,6 @@
class Link
{
Link next;
int data;
Link(int a_data, Link a_next) { next = a_next; data = a_data; }
}

View file

@ -0,0 +1 @@
Link small_primes = new Link(2, new Link(3, new Link(5, new Link(7, null))));

View file

@ -0,0 +1,6 @@
class Link<T>
{
Link<T> next;
T data;
Link(T a_data, Link<T> a_next) { next = a_next; data = a_data; }
}

View file

@ -0,0 +1,30 @@
function LinkedList(value, next) {
this._value = value;
this._next = next;
}
LinkedList.prototype.value = function() {
if (arguments.length == 1)
this._value = arguments[0];
else
return this._value;
}
LinkedList.prototype.next = function() {
if (arguments.length == 1)
this._next = arguments[0];
else
return this._next;
}
// convenience function to assist the creation of linked lists.
function createLinkedListFromArray(ary) {
var head = new LinkedList(ary[0], null);
var prev = head;
for (var i = 1; i < ary.length; i++) {
var node = new LinkedList(ary[i], null);
prev.next(node);
prev = node;
}
return head;
}
var head = createLinkedListFromArray([10,20,30,40]);

View file

@ -0,0 +1,5 @@
fput item list ; add item to the head of a list
first list ; get the data
butfirst list ; get the remainder
bf list ; contraction for "butfirst"

View file

@ -0,0 +1,2 @@
.setfirst list value
.setbf list remainder

View file

@ -0,0 +1,6 @@
TYPE
Link = POINTER TO LinkRcd;
LinkRcd = RECORD
Next: Link;
Data: INTEGER
END;

View file

@ -0,0 +1 @@
type 'a list = Nil | Cons of 'a * 'a list

View file

@ -0,0 +1 @@
type int_list = Nil | Cons of int * int_list ref

View file

@ -0,0 +1,42 @@
#import <objc/Object.h>
@interface RCListElement : Object
{
RCListElement *next;
id datum;
}
+ (RCListElement *)new;
- (RCListElement *)next;
- (id)datum;
- (RCListElement *)setNext: (RCListElement *)nx;
- (void)setDatum: (id)d;
@end
@implementation RCListElement
+ (RCListElement *)new
{
RCListElement *m = [super new];
[m setNext: nil];
[m setDatum: nil];
return m;
}
- (RCListElement *)next
{
return next;
}
- (id)datum
{
return datum;
}
- (RCListElement *)setNext: (RCListElement *)nx
{
RCListElement *p;
p = next;
next = nx;
return p;
}
- (void)setDatum: (id)d
{
datum = d;
}
@end

View file

@ -0,0 +1,6 @@
type
PLink = ^TLink;
TLink = record
FNext: PLink;
FData: integer;
end;

View file

@ -0,0 +1 @@
my $elem = 42 => $nextelem;

View file

@ -0,0 +1,5 @@
my %node = (
data => 'say what',
next => \%foo_node,
);
$node{next} = \%bar_node; # mutable

View file

@ -0,0 +1,15 @@
;;; Use shorthand syntax to create list.
lvars l1 = [1 2 three 'four'];
;;; Allocate a single list node, with value field 1 and the link field
;;; pointing to empty list
lvars l2 = cons(1, []);
;;; print first element of l1
front(l1) =>
;;; print the rest of l1
back(l1) =>
;;; Use index notation to access third element
l1(3) =>
;;; modify link field of l2 to point to l1
l1 -> back(l2);
;;; Print l2
l2 =>

View file

@ -0,0 +1,16 @@
uses objectclass;
define :class ListNode;
slot value = [];
slot next = [];
enddefine;
;;; Allocate new node and assign to l1
newListNode() -> l1;
;;; Print it
l1 =>
;;; modify value
1 -> value(l1);
l1 =>
;;; Allocate new node with initialized values and assign to link field
;;; of l1
consListNode(2, []) -> next(l1);
l1 =>

View file

@ -0,0 +1,4 @@
Structure MyData
*next.MyData
Value.i
EndStructure

View file

@ -0,0 +1,27 @@
class LinkedList(object):
"""USELESS academic/classroom example of a linked list implemented in Python.
Don't ever consider using something this crude! Use the built-in list() type!
"""
class Node(object):
def __init__(self, item):
self.value = item
self.next = None
def __init__(self, item=None):
if item is not None:
self.head = Node(item); self.tail = self.head
else:
self.head = None; self.tail = None
def append(self, item):
if not self.head:
self.head = Node(item)
self.tail = self.head
elif self.tail:
self.tail.next = Node(item)
self.tail = self.tail.next
else:
self.tail = Node(item)
def __iter__(self):
cursor = self.head
while cursor:
yield cursor.value
cursor = cursor.next

View file

@ -0,0 +1,34 @@
/*REXX program to show how to create and show a single-linked list. */
@.=0 /*define a null linked list (so far). */
call set@ ,3 /*build linked list of 12 proth primes.*/
call set@ ,5
call set@ ,13
call set@ ,17
call set@ ,41
call set@ ,97
call set@ ,113
call set@ ,193
call set@ ,241
call set@ ,257
call set@ ,353
call set@ ,449
w=length(@._last) /*use width of the last item number. */
do j=1 for @._last /*show all entries of the linked list.*/
say "item" right(j,w) '=' right(@.j._value,@.max_width)
end /*j*/
exit /*stick a fork in it, we're done. */
/*───────────────────────────────SET@ subroutine────────────────────────*/
set@: procedure expose @.; parse arg #,y
if arg(1,'o') then do /*if 1st arg omitted, then add to list*/
_=@._last+1 /*bump the last ptr in the linked list*/
@._last=_ /*define the next item in linked list.*/
#=_ /*point to this item in linked list.*/
end
@.#._value=y /*set the item to the value specified.*/
@.max_width=max(@.max_width,length(y)) /*set maximum width of any value.*/
if #\==1 then do /*if not the first item, link it. */
prev=#-1 /*figure out what the previous item is*/
@.prev._next=# /*now, link the previous item to here.*/
end
return /*return to the invoker of this sub. *//

View file

@ -0,0 +1,28 @@
class ListNode
attr_accessor :value, :succ
def initialize(value, succ=nil)
self.value = value
self.succ = succ
end
def each(&b)
yield self
succ.each(&b) if succ
end
include Enumerable
def self.from_array(ary)
head = self.new(ary[0], nil)
prev = head
ary[1..-1].each do |val|
node = self.new(val, nil)
prev.succ = node
prev = node
end
head
end
end
list = ListNode.from_array([1,2,3,4])

View file

@ -0,0 +1,3 @@
data = 10
link = 10
dim node{data,link}

View file

@ -0,0 +1,4 @@
enum SingleLinkedList<T> {
Node(T, @mut SingleLinkedList<T>),
None
}

View file

@ -0,0 +1,4 @@
class Node(n: Int, link: Node) {
var data = n
var next = link
}

View file

@ -0,0 +1,11 @@
class Node {
var data: Int
var next = this
def this(n: Int, link: Node) {
this()
if (next != null){
data = n
next = link
}
}

View file

@ -0,0 +1,2 @@
(car my-list) ; returns the first element of the list
(cdr my-list) ; returns the remainder of the list

View file

@ -0,0 +1,2 @@
(set-car! my-list new-elem)
(set-cdr! my-list new-next)

View file

@ -0,0 +1,25 @@
oo::class create List {
variable content next
constructor {value {list ""}} {
set content $value
set next $list
}
method value args {
set content {*}$args
}
method attach {list} {
set next $list
}
method detach {} {
set next ""
}
method next {} {
return $next
}
method print {} {
for {set n [self]} {$n ne ""} {set n [$n next]} {
lappend values [$n value]
}
return $values
}
}

View file

@ -0,0 +1,4 @@
struct link
.next: resd 1
.data: resd 1
endstruc

View file

@ -0,0 +1,4 @@
link struct
next dd ?
data dd ?
link ends

View file

@ -0,0 +1,5 @@
struc link next,data
{
.next dd next
.data dd data
}

View file

@ -0,0 +1,16 @@
include c:\cxpl\codes; \intrinsic 'code' declarations
def IntSize=4; \number of bytes in an integer
def Size=10; \number of nodes in this linked list
int Link, List, Node;
[Link:= 0; \build linked list, starting at the end
for Node:= 0 to Size-1 do
[List:= Reserve(IntSize*2); \get some memory to hold link and data
List(0):= Link;
List(1):= Node*Node; \insert example data
Link:= List; \Link now points to newly created node
];
Node:= List; \traverse the linked list
repeat IntOut(0, Node(1)); CrLf(0); \display the example data
Node:= Node(0); \move to next node
until Node=0; \end of the list
]