Doubly-linked list/Element insertion: Difference between revisions

m
→‎{{header|REXX}}: changed the style of the output. -- ~~~~
mNo edit summary
m (→‎{{header|REXX}}: changed the style of the output. -- ~~~~)
Line 724:
=={{header|REXX}}==
REXX doesn't have linked lists, as there are no pointers (or handles).
<br>However, linked lists can be simulated with lists in REXX.
<br>
<lang rexx>/*REXX program that implements various List Manager functions. */
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 @init
 
call sy 'building list: Was it a cat I saw'; call @put 'Was it a cat I saw'
 
call sy 'displaying list size.' ; call sysay 'initializinglist the list.size='@size()
call sy 'forward list' ; call sy 'showing list'@show
call @init
call sy 'backward list' ; call sy@show 'building list: Was it a cat I saw',,-1
call sy 'showing 4th item' ; call @show 4,1
call @put 'Was it a cat I saw'
call sy 'showing 6th & 6th items' ; call sy@show 'displaying list size.'5,2
call sy 'adding item before item 4: black' ; call @put 'black',4
say 'list size='@size()
call sy 'showing list' ; call sy 'forward list'@show
call sy 'adding to tail: there, in the ...'; call @put 'there, in the shadows, stalking its prey (and next meal).'
call @show
call sy 'showing list' ; call sy 'backward list'@show
call sy 'adding to head: Oy!' ; call @put 'Oy!',0
call @show ,,-1
call sy 'showing list' ; call sy 'showing 4th item'@show
exit call sy 'adding to tail: there, /*stick a fork in theit, ...we're done.*/
call @show 4,1
/*──────────────────────────────────subroutines─────────────────────────*/
call sy 'showing 6th & 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
 
 
/*===========================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 796 ⟶ 777:
return
 
@show: procedure expose $.; parse arg k,m,dir
if dir==-1 & k=='' then k=$.#
m=p(m $.#);
Line 803 ⟶ 784:
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 817 ⟶ 798:
return 0
 
@del: procedure expose $.; arg k,m
call @parms 'km'
_=subword($.@,k,k-1) subword($.@,k+m)
Line 823 ⟶ 804:
call @adjust
return</lang>
'''output'''
Output:
<pre style="height:50ex;overflow:scroll">
<pre>
---─── 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>