{ From user Lonnie, Model Untitled at Wed, Sep 05, 2007 3:41 PM} Softwareversion 4.0.0 Linklibrary Linked_list_library Title: Linked List Library Description: Contains routines for manipulating a linked list.~ The simplest linked list is just NULL -- a linked list with zero elem~~ ents.~ Any other linked list is a reference to a record indexed by Linked_Li~~ st with each cell containing a reference, to the element, and to the ~~ remainder.~ ~ Use the LL_Push to build the list.~ After a list is built, the easiest way to use and view it is to conve~~ rt it back to an array using LL_to_RArray.~ ~ For example, assume you have a function IsPrime(n).~ This collects all the primes between 2 and n:~ ~ var LL := null;~ for i:=n..2 do begin~ if IsPrime(i) then LL := LL_Push(LL,i) /* Build the list */~ end;~ LL_to_Array( LL, "primes" ) /* Convert to an array for easy use */ Author: Lonnie Chrisman Date: Mon, Aug 18, 2003 11:53 PM Saveauthor: Lonnie Savedate: Wed, Sep 05, 2007 3:41 PM Defaultsize: 48,24 Nodesize: 48,32 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Diagstate: 1,354,40,527,421,17 Windstate: 2,102,90,669,534 Fileinfo: 0,Linklibrary Linked_list_library,2,2,0,0,C:\Src\AnalyticaDe~~ velopment\Analytica\ExecDebug\Libraries\Linked List Library.ANA Index Linked_list Title: Linked List Description: Record definition of a linked list. Item contains a refe~~ rence to the element. Next contains the remainder of the linked list~~ , which is either NULL or a reference to something indexed by Linked_~~ List. Definition: ['Item','Next'] Nodelocation: 176,32,1 Nodesize: 48,24 Function Ll_push(LL,x) Title: LL Push Description: Pushes x on the front of the linked list LL and returns t~~ he new LL. Definition: \Array(Linked_List,[\x,LL]) Nodelocation: 176,96,1 Nodesize: 48,24 Windstate: 2,20,466,476,224 Paramnames: LL,x Function Ll_to_array(LL,indexName:atomic) Title: LL To Array Description: Converts a linked list to an array. ~ The array is indexed by a single local index having the name indicate~~ d by the second parameter. Definition: var len := LL_Length(LL);~ if len=0 then~ NULL~ else begin~ index J / indexName :=1..LL_Length(LL);~ for i:=J Do (~ Var x:=LL_First(LL);~ LL := LL_Remove_First(LL);~ x~ )~ end Nodelocation: 288,160,1 Nodesize: 52,24 Windstate: 2,426,58,593,411 Paramnames: LL,indexName Function Ll_length(LL:atomic) Title: LL Length Description: Returnst the # of elements in a linked list. Definition: var len:=0;~ while (IsReference(LL)) begin~ LL := subscript(#LL,Linked_List,"Next");~ len := len + 1~ end;~ len Nodelocation: 176,160,1 Nodesize: 48,24 Windstate: 2,142,87,476,224 Paramnames: LL Function Ll_first(LL) Title: LL First Description: Returns the first element of a non-empty linked list. Definition: #Subscript(#LL,Linked_List,"Item") Nodelocation: 176,224,1 Nodesize: 48,24 Paramnames: LL Function Ll_remove_first(LL) Title: LL Remove First Description: Returns a linked list resulting from the removal of the f~~ irst element of the list. Definition: Subscript(#LL,Linked_List,"Next") Nodelocation: 288,224,1 Nodesize: 48,24 Paramnames: LL Function Ll_to_rarray(LL,indexName:atomic) Title: LL to RArray Description: Converts a linked list to an array but with elements in r~~ everse order (last in LL first in array).~ This is often useful when collecting things during an iteration, wher~~ e pushing them on the front is most convenient, so that the linked li~~ st is actually in reverse order. When done, this retrieves the colle~~ ction as an Analytica array in the forward iteration order, as if eac~~ h item had been appended during the iteration. Definition: var len := LL_Length(LL);~ index J / indexName :=1..len;~ Slice( for i:=J Do (~ Var x:=LL_First(LL);~ LL := LL_Remove_First(LL);~ x~ ), J, len-J+1) Nodelocation: 288,96,1 Nodesize: 48,24 Windstate: 2,102,90,631,415 Paramnames: LL,indexName Function Ll_nth(LL ; n:atomic positive) Title: LL Nth Description: Returns the Nth element in a linked list. ~ Note that this operation is not efficient -- if you are going to do i~~ t many times, you may be better off converting to an array first. El~~ ement n=1 is the first element. If n does not correspond to an eleme~~ nt, NULL is returned. Definition: while Isreference(LL) and n>1 Do begin~ n:=n-1; ~ LL:=LL_Remove_First(LL)~ end;~ if Isreference(LL) then LL_First(LL)~ else NULL Nodelocation: 176,288,1 Nodesize: 48,24 Windstate: 2,445,158,472,331 Paramnames: LL,n Module Ll_example_usage Title: Example usage Description: This example shows a linked list of 'fruits'. It current~~ ly exercises all the functions of the 'linked list library'. Author: Fred & Lonnie Date: Mon, Feb 28, 2005 2:48 PM Saveauthor: Lonnie Savedate: Wed, Mar 16, 2005 10:00 AM Defaultsize: 48,24 Nodelocation: 408,80,1 Nodesize: 48,24 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Diagstate: 1,377,93,664,512,17 Windstate: 2,44,283,476,224 Diagramcolor: 52427,65535,62258 Variable First_tweaked_call Title: First Tweaked Definition: Ll_first( The_ll ) Nodelocation: 336,232,1 Nodesize: 72,16 Valuestate: 2,40,50,416,303,0,MIDM Variable Fruit_0 Title: Fruit 0 Definition: 'pineapple' Nodelocation: 488,272,1 Nodesize: 48,24 Defnstate: 2,43,180,416,303,0,MIDM Nodecolor: 65535,57341,1 Variable Fruit_1 Title: Fruit 1 Definition: 'apple' Nodelocation: 104,240,1 Nodesize: 48,24 Defnstate: 2,49,215,416,303,0,MIDM Valuestate: 2,56,66,416,303,0,MIDM Nodecolor: 65535,1,1 Variable Fruit_2 Title: Fruit 2 Definition: 'banana' Nodelocation: 104,176,1 Nodesize: 48,24 Defnstate: 2,368,120,416,303,0,MIDM Valuestate: 2,275,145,416,303,0,MIDM Nodecolor: 65535,65531,1 Variable Fruit_3 Title: Fruit 3 Definition: 'cranberry' Nodelocation: 104,112,1 Nodesize: 48,24 Nodecolor: 52427,1,1 Variable Fruit_4 Title: Fruit 4 Definition: 'durian' Nodelocation: 104,48,1 Nodesize: 48,24 Defnstate: 2,215,222,416,303,0,MIDM Valuestate: 2,72,82,416,303,0,MIDM Nodecolor: 32770,65535,1 Variable Length_call Title: Length Definition: Ll_length( The_ll ) Nodelocation: 336,152,1 Nodesize: 72,16 Valuestate: 2,56,66,416,303,0,MIDM Variable Ll_to_array1 Title: LL to Array Definition: Ll_to_array( The_ll, "Fruits" ) Nodelocation: 336,72,1 Nodesize: 72,16 Valuestate: 2,69,66,416,303,0,MIDM Variable Ll_to_rarray_call Title: LL to RArray Definition: Ll_to_rarray( The_ll, "Fruits") Nodelocation: 336,112,1 Nodesize: 72,16 Valuestate: 2,314,224,416,303,0,MIDM Variable The_ll Title: The LL Definition: var LL := LL_Push(null,Fruit_4);~ LL := LL_Push(LL,Fruit_3);~ LL := LL_Push(LL,Fruit_2);~ LL_Push(LL,Fruit_1) Nodelocation: 104,304,1 Nodesize: 48,24 Valuestate: 2,40,50,416,303,0,MIDM Variable Push_function_call Title: Push function call Definition: Ll_push( The_ll, Fruit_0 ) Nodelocation: 336,272,1 Nodesize: 72,16 Valuestate: 2,213,154,416,303,0,MIDM Variable Remove_call Title: Remove Definition: Ll_remove_first( The_ll ) Nodelocation: 336,312,1 Nodesize: 68,16 Valuestate: 2,88,98,416,303,0,MIDM Variable Ll_nth_call Title: LL Nth Definition: Ll_nth( The_ll, 3) Nodelocation: 336,192,1 Nodesize: 68,16 Valuestate: 2,40,50,416,303,0,MIDM Function Isprime(n : atomic numeric) Title: IsPrime Description: Returns true if n is a prime. Definition: if n<2 then false~ else if n=2 then true ~ else if Mod(n,2)=0 then false~ else begin~ var k:=3;~ var last := sqrt(n);~ var factorFound := false;~ while ( not factorFound and k<=last ) (~ if Mod(n,k)=0 then factorFound:=true;~ k := k+2;~ );~ not factorFound~ end Nodelocation: 104,392,1 Nodesize: 48,24 Paramnames: n Function First_n_primes(N : atomic positive) Title: First N Primes(N) Description: Returns a list of the first N primes.~ This is an example demonstrating how the linked list library might be~~ used. Definition: var nfound := 1;~ var LL := LL_Push(null,2);~ var i:=3;~ /* Builds a linked list of the primes found so far */~ while (nfound