Doubly-linked list/Element insertion: Difference between revisions
Content added Content deleted
mNo edit summary |
m (→{{header|REXX}}: changed the style of the output. -- ~~~~) |
||
Line 724: | Line 724: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
REXX doesn't have linked lists, as there are no pointers (or handles). |
REXX doesn't have linked lists, as there are no pointers (or handles). |
||
⚫ | |||
<br> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
/*┌────────────────────────────────────────────────────────────────────┐ |
/*┌────────────────────────────────────────────────────────────────────┐ |
||
┌─┘ Functions of the List Manager └─┐ |
┌─┘ Functions of the List Manager └─┐ |
||
│ │ |
│ │ |
||
│ @init |
│ @init ─── initializes the List. │ |
||
│ │ |
│ │ |
||
│ @size |
│ @size ─── returns the size of the List [could be 0 (zero)]. │ |
||
│ │ |
│ │ |
||
│ @show |
│ @show ─── shows (displays) the complete List. │ |
||
│ @show k,1 |
│ @show k,1 ─── shows (displays) the Kth item. │ |
||
│ @show k,m |
│ @show k,m ─── shows (displays) M items, starting with Kth item. │ |
||
│ @show ,, |
│ @show ,,─1 ─── shows (displays) the complete List backwards. │ |
||
│ │ |
│ │ |
||
│ @get k |
│ @get k ─── returns the Kth item. │ |
||
│ @get k,m |
│ @get k,m ─── returns the M items starting with the Kth item. │ |
||
│ │ |
│ │ |
||
│ @put x |
│ @put x ─── adds the X items to the end (tail) of the List. │ |
||
│ @put x,0 |
│ @put x,0 ─── adds the X items to the start (head) of the List. │ |
||
│ @put x,k |
│ @put x,k ─── adds the X items to before of the Kth item. │ |
||
│ │ |
│ │ |
||
│ @del k |
│ @del k ─── deletes the item K. │ |
||
│ @del k,m |
│ @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.' ; say 'list size='@size() |
|||
⚫ | |||
call @init |
|||
call sy 'backward list' ; call @show ,,-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 @show 5,2 |
|||
⚫ | |||
say 'list size='@size() |
|||
call sy 'showing list' ; call @show |
|||
⚫ | |||
call @show |
|||
call sy 'showing list' ; call @show |
|||
call sy 'adding to head: Oy!' ; call @put 'Oy!',0 |
|||
call @show ,,-1 |
|||
call sy 'showing list' ; call @show |
|||
⚫ | |||
call @show 4,1 |
|||
/*──────────────────────────────────subroutines─────────────────────────*/ |
|||
call sy 'showing 6th & 6th items' |
|||
call @show 5,2 |
|||
⚫ | |||
call @put 'black',4 |
|||
⚫ | |||
call @show |
|||
⚫ | |||
⚫ | |||
call sy 'showing list' |
|||
call @show |
|||
call sy 'adding to head: Oy!' |
|||
call @put 'Oy!',0 |
|||
call sy 'showing list' |
|||
call @show |
|||
exit |
|||
/*===========================subroutines================================*/ |
|||
⚫ | |||
p: return word(arg(1),1) |
p: return word(arg(1),1) |
||
⚫ | |||
@hasopt: arg o; return pos(o,opt)\==0 |
@hasopt: arg o; return pos(o,opt)\==0 |
||
@size: return $.# |
@size: return $.# |
||
Line 796: | Line 777: | ||
return |
return |
||
@show: procedure expose $.; parse arg k,m,dir |
@show: procedure expose $.; parse arg k,m,dir |
||
if dir==-1 & k=='' then k=$.# |
if dir==-1 & k=='' then k=$.# |
||
m=p(m $.#); |
m=p(m $.#); |
||
Line 803: | Line 784: | ||
return 0 |
return 0 |
||
@get: procedure expose $.; arg k,m,dir,_ |
@get: procedure expose $.; arg k,m,dir,_ |
||
call @parms 'kmd' |
call @parms 'kmd' |
||
do j=k for m by dir while j>0 & j<=$.# |
do j=k for m by dir while j>0 & j<=$.# |
||
_=_ subword($.@,j,1) |
_=_ subword($.@,j,1) |
||
end |
end /*j*/ |
||
return strip(_) |
return strip(_) |
||
@put: procedure expose $.; parse arg x,k |
@put: procedure expose $.; parse arg x,k |
||
k=p(k $.#+1) |
k=p(k $.#+1) |
||
call @parms 'k' |
call @parms 'k' |
||
Line 817: | Line 798: | ||
return 0 |
return 0 |
||
@del: procedure expose $.; arg k,m |
@del: procedure expose $.; arg k,m |
||
call @parms 'km' |
call @parms 'km' |
||
_=subword($.@,k,k-1) subword($.@,k+m) |
_=subword($.@,k,k-1) subword($.@,k+m) |
||
Line 823: | Line 804: | ||
call @adjust |
call @adjust |
||
return</lang> |
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 |
list size=6 |
||
─── forward list ─── |
|||
Was it a cat I saw |
Was it a cat I saw |
||
─── backward list ─── |
|||
saw I cat a it Was |
saw I cat a it Was |
||
─── showing 4th item ─── |
|||
cat |
cat |
||
─── showing 6th & 6th items ─── |
|||
I saw |
I saw |
||
─── adding item before item 4: black ─── |
|||
─── showing list ─── |
|||
Was it a black cat I saw |
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). |
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). |
Oy! Was it a black cat I saw there, in the shadows, stalking its prey (and next meal). |
||
</pre> |
</pre> |