Jump to content

Doubly-linked list/Traversal: Difference between revisions

m
→‎{{header|REXX}}: added whitespace, added DO-END comment label, indented DO loop. -- ~~~~
mNo edit summary
m (→‎{{header|REXX}}: added whitespace, added DO-END comment label, indented DO loop. -- ~~~~)
Line 846:
REXX doesn't have linked lists, as there are no pointers (or handles).
However, linked lists can be simulated with lists in REXX.
<lang rexx>/*REXX program that implements various List Manager functions. */
 
/*┌────────────────────────────────────────────────────────────────────┐
┌─┘ Functions of the List Manager └─┐
│ │
│ @init ---─── initializes the List. │
│ │
│ @size ---─── returns the size of the List [could be 0 (zero)]. │
│ │
│ @show ---─── shows (displays) the complete List. │
│ @show k,1 ---─── shows (displays) the Kth item. │
│ @show k,m ---─── shows (displays) M items, starting with Kth item. │
│ @show ,,-1─1 ---─── shows (displays) the complete List backwards. │
│ │
│ @get k ---─── returns the Kth item. │
│ @get k,m ---─── returns the M items starting with the Kth item. │
│ │
│ @put x ---─── adds the X items to the end (tail) of the List. │
│ @put x,0 ---─── adds the X items to the start (head) of the List. │
│ @put x,k ---─── adds the X items to before of the Kth item. │
│ │
│ @del k ---─── deletes the item K. │
│ @del k,m ---─── deletes the M items starting with item K. │
└─┐ ┌─┘
└────────────────────────────────────────────────────────────────────┘*/
call sy 'initializing the list.'
 
 
call sy 'initializing the list.'
call @init
call sy 'building list: Was it a cat I saw'
Line 899 ⟶ 896:
call sy 'showing list'
call @show
exit /*stick a fork in it, we're done.*/
exit
/*──────────────────────────────────subroutines─────────────────────────*/
 
 
/*===========================subroutines================================*/
sy: say; say left('',30) "---" arg(1) '---'; return
p: return word(arg(1),1)
sy: say; say left('',30) "---───" arg(1) '---───'; return
@hasopt: arg o; return pos(o,opt)\==0
@size: return $.#
Line 916 ⟶ 911:
return
 
@show: procedure expose $.; parse arg k,m,dir
if dir==-1 & k=='' then k=$.#
m=p(m $.#);
Line 923 ⟶ 918:
return 0
 
@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(_)
 
@put: procedure expose $.; parse arg x,k
k=p(k $.#+1)
call @parms 'k'
Line 937 ⟶ 932:
return 0
 
@del: procedure expose $.; arg k,m
call @parms 'km'
_=subword($.@,k,k-1) subword($.@,k+m)
$.@=_
call @adjust
return</lang>
</lang>
Output:
'''output'''
<pre>
<pre style="height:50ex;overflow:scroll">
---─── initializing the list. ---───
 
---─── building list: Was it a cat I saw ---───
 
---─── displaying list size. ---───
list size=6
 
---─── forward list ---───
Was it a cat I saw
 
---─── backward list ---───
saw I cat a it Was
 
---─── showing 4th item ---───
cat
 
---─── showing 6th & 6th items ---───
I saw
 
---─── adding item before item 4: black ---───
 
---─── showing list ---───
Was it a black cat I saw
 
---─── adding to tail: there, in the ... ---───
 
---─── showing list ---───
Was it a black cat I saw there, in the shadows, stalking its prey (and next meal).
 
---─── adding to head: Oy! ---───
 
---─── showing list ---───
Oy! Was it a black cat I saw there, in the shadows, stalking its prey (and next meal).
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.