Doubly-linked list/Element definition: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed boxed comments.)
m (→‎{{header|REXX}}: add/changed comments and whitespace.)
Line 677: Line 677:
║ @del k,m ─── deletes the M items starting with item K. ║
║ @del k,m ─── deletes the M items starting with item K. ║
╚═════════════════════════════════════════════════════════════════════════╝
╚═════════════════════════════════════════════════════════════════════════╝
<lang rexx>/*REXX program that implements various List Manager functions. */
<lang rexx>/*REXX program implements various List Manager functions (see the documentation above).*/
call sy 'initializing the list.' ; call @init
call sy 'initializing the list.' ; call @init
call sy 'building list: Was it a cat I saw'; call @put 'Was it a cat I saw'
call sy 'building list: Was it a cat I saw' ; call @put "Was it a cat I saw"
call sy 'displaying list size.' ; say 'list size='@size()
call sy 'displaying list size.' ; say "list size="@size()
call sy 'forward list' ; call @show
call sy 'forward list' ; call @show
call sy 'backward list' ; call @show ,,-1
call sy 'backward list' ; call @show ,,-1
call sy 'showing 4th item' ; call @show 4,1
call sy 'showing 4th item' ; call @show 4,1
call sy 'showing 5th & 6th items' ; call @show 5,2
call sy 'showing 5th & 6th items' ; call @show 5,2
call sy 'adding item before item 4: black' ; call @put 'black',4
call sy 'adding item before item 4: black' ; call @put "black",4
call sy 'showing list' ; call @show
call sy 'showing list' ; call @show
call sy 'adding to tail: there, in the ...'; call @put 'there, in the shadows, stalking its prey (and next meal).'
call sy 'adding to tail: there, in the ...' ; call @put "there, in the shadows, stalking its prey (and next meal)."
call sy 'showing list' ; call @show
call sy 'showing list' ; call @show
call sy 'adding to head: Oy!' ; call @put 'Oy!',0
call sy 'adding to head: Oy!' ; call @put "Oy!",0
call sy 'showing list' ; call @show
call sy 'showing list' ; call @show
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────subroutines─────────────────────────*/
p: return word(arg(1),1)
p: return word(arg(1), 1) /*pick the first word out of many items*/
sy: say; say left('',30) "───" arg(1) '───'; return
sy: say; say left('', 30) "───" arg(1) '───'; return
@init: $.@=; @adjust: $.@=space($.@); $.#=words($.@); return
@hasopt: arg o; return pos(o,opt)\==0
@hasopt: arg o; return pos(o, opt)\==0
@size: return $.#
@size: return $.#
/*──────────────────────────────────────────────────────────────────────────────────────*/
@init: $.@=''; $.#=0; return 0
@del: procedure expose $.; arg k,m; call @parms 'km'
@adjust: $.@=space($.@); $.#=words($.@); return 0
_=subword($.@, k, k-1) subword($.@, k+m)

$.@=_; call @adjust; return
@parms: arg opt
/*──────────────────────────────────────────────────────────────────────────────────────*/
if @hasopt('k') then k=min($.#+1,max(1,p(k 1)))
if @hasopt('m') then m=p(m 1)
@get: procedure expose $.; arg k,m,dir,_
if @hasopt('d') then dir=p(dir 1)
return

@show: procedure expose $.; parse arg k,m,dir
if dir==-1 & k=='' then k=$.#
m=p(m $.#);
call @parms 'kmd'
call @parms 'kmd'
do j=k for m by dir while j>0 & j<=$.#
say @get(k,m,dir);
_=_ subword($.@, j, 1)
return 0
end /*j*/

@get: procedure expose $.; arg k,m,dir,_
call @parms 'kmd'
do j=k for m by dir while j>0 & j<=$.#
_=_ subword($.@,j,1)
end /*j*/
return strip(_)
return strip(_)
/*──────────────────────────────────────────────────────────────────────────────────────*/

@parms: arg opt /*define a variable based on an option.*/
@put: procedure expose $.; parse arg x,k
k=p(k $.#+1)
if @hasopt('k') then k=min($.#+1, max(1, p(k 1)))
call @parms 'k'
if @hasopt('m') then m=p(m 1)
if @hasopt('d') then dir=p(dir 1); return
$.@=subword($.@,1,max(0,k-1)) x subword($.@,k)
/*──────────────────────────────────────────────────────────────────────────────────────*/
call @adjust
@put: procedure expose $.; parse arg x,k; k=p(k $.#+1); call @parms 'k'
return 0
$.@=subword($.@, 1, max(0, k-1)) x subword($.@, k); call @adjust

@del: procedure expose $.; arg k,m
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
call @parms 'km'
@show: procedure expose $.; parse arg k,m,dir; if dir==-1 & k=='' then k=$.#
_=subword($.@,k,k-1) subword($.@,k+m)
m=p(m $.#); call @parms 'kmd'; say @get(k,m, dir); return</lang>
$.@=_
call @adjust
return</lang>
'''output'''
'''output'''
<pre style="height:30ex">
<pre style="height:30ex">