Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -0,0 +1,27 @@
|
|||
type Node{T}
|
||||
data::T
|
||||
next::Node{T}
|
||||
|
||||
function Node(data::T)
|
||||
n = new()
|
||||
n.data = data
|
||||
# mark the end of the list. Julia does not have nil or null.
|
||||
n.next = n
|
||||
n
|
||||
end
|
||||
end
|
||||
|
||||
# convenience. Let use write Node(10) or Node(10.0) instead of Node{Int64}(10), Node{Float64}(10.0)
|
||||
function Node(data)
|
||||
return Node{typeof(data)}(data)
|
||||
end
|
||||
|
||||
islast(n::Node) = (n == n.next)
|
||||
|
||||
function append{T}(n::Node{T}, data::T)
|
||||
tmp = Node(data)
|
||||
if !islast(n)
|
||||
tmp.next = n.next
|
||||
end
|
||||
n.next = tmp
|
||||
end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
head = Node(1)
|
||||
n = append(head, 2)
|
||||
n = append(n, 3)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
declare 1 node based (p),
|
||||
2 value fixed,
|
||||
2 link pointer;
|
||||
|
|
@ -1,34 +1,38 @@
|
|||
/*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.*/
|
||||
/*REXX program demonstrates how to create and show a single-linked list.*/
|
||||
@.=0 /*define a null linked list. */
|
||||
call set@ 3 /*linked list: 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=@.max_width /*use the maximum width of nums. */
|
||||
call list@ /*list all the elements in list. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────LIST@ subroutine────────────────────*/
|
||||
list@: say; w=max(7, @.max_width ) /*use the max width of nums or 7.*/
|
||||
say center('item',6) center('value',w) center('next',6)
|
||||
say center('' ,6,'─') center('' ,w,'─') center('' ,6,'─')
|
||||
p=1
|
||||
do j=1 until p==0 /*show all entries of linked list*/
|
||||
say right(j,6) right(@.p._value,w) right(@.p._next,6)
|
||||
p=@.p._next
|
||||
end /*j*/
|
||||
return
|
||||
/*──────────────────────────────────SET@ subroutine─────────────────────*/
|
||||
set@: procedure expose @.; parse arg y /*get element to be added to list*/
|
||||
_=@._last /*set the previous last element. */
|
||||
n=_+1 /*bump last ptr in linked list. */
|
||||
@._._next=n /*set the next pointer value. */
|
||||
@._last=n /*define next item in linked list*/
|
||||
@.n._value=y /*set item to the value specified*/
|
||||
@.n._next=0 /*set the next pointer value. */
|
||||
@..y=n /*set a locator pointer to self. */
|
||||
@.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. *//
|
||||
return /*return to invoker of this sub. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue