Doubly-linked list/Element definition: Difference between revisions

m
→‎{{header|REXX}}: add/changed comments and whitespace.
m (→‎{{header|REXX}}: changed boxed comments.)
m (→‎{{header|REXX}}: add/changed comments and whitespace.)
Line 677:
║ @del k,m ─── deletes the M items starting with item K. ║
╚═════════════════════════════════════════════════════════════════════════╝
<lang rexx>/*REXX program that implements various List Manager functions. (see the documentation above).*/
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 'displaying list size.' ; say ' "list size='"@size()
call sy 'forward list' ; call @show
call sy 'backward list' ; call @show ,,-1
call sy 'showing 4th item' ; call @show 4,1
call sy 'showing 5th & 6th items' ; call @show 5,2
call sy 'adding item before item 4: black' ; call @put '"black'",4
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 'showing list' ; call @show
call sy 'adding to head: Oy!' ; call @put '"Oy!'",0
call sy 'showing list' ; call @show
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────subroutines─────────────────────────*/
p: return word(arg(1), 1) /*pick the first word out of many items*/
sy: say; say left('', 30) "───" arg(1) '───'; return
@init: $.@=; @adjust: $.@=space($.@); $.#=words($.@); return 0 return
@hasopt: arg o; return pos(o, opt)\==0
@size: return $.#
/*──────────────────────────────────────────────────────────────────────────────────────*/
@init: $.@=''; $.#=0; return 0
@getdel: procedure expose $.; arg k,m,dir,_; 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)))
@get: procedure expose $.; if @hasopt('m') thenarg k,m=p(m 1),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'
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(_)
/*──────────────────────────────────────────────────────────────────────────────────────*/
 
@parms: arg opt /*define a variable based on an option.*/
@put: procedure expose $.; parse arg x,k
k=pif @hasopt('k') then k=min($.#+1, max(1, p(k 1)))
callif @parms hasopt('km') 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,mreturn
/*──────────────────────────────────────────────────────────────────────────────────────*/
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'''
<pre style="height:30ex">