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>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 └─┐
┌─┘ Functions of the List Manager └─┐
│ │
│ │
│ @init --- initializes the List. │
│ @init ─── initializes the List. │
│ │
│ │
│ @size --- returns the size of the List [could be 0 (zero)]. │
│ @size ─── returns the size of the List [could be 0 (zero)]. │
│ │
│ │
│ @show --- shows (displays) the complete List. │
│ @show ─── shows (displays) the complete List. │
│ @show k,1 --- shows (displays) the Kth item. │
│ @show k,1 ─── shows (displays) the Kth item. │
│ @show k,m --- shows (displays) M items, starting with Kth item. │
│ @show k,m ─── shows (displays) M items, starting with Kth item. │
│ @show ,,-1 --- shows (displays) the complete List backwards. │
│ @show ,,─1 ─── shows (displays) the complete List backwards. │
│ │
│ │
│ @get k --- returns the Kth item. │
│ @get k ─── returns the Kth item. │
│ @get k,m --- returns the M items starting with 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 ─── 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,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. │
│ @put x,k ─── adds the X items to before of the Kth item. │
│ │
│ │
│ @del k --- deletes the item K. │
│ @del k ─── deletes the item K. │
│ @del k,m --- deletes the M items starting with 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 'initializing the list.'
call sy 'displaying list size.' ; say 'list size='@size()
call sy 'forward list' ; call @show
call @init
call sy 'building list: Was it a cat I saw'
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 'displaying list size.'
call sy 'showing 6th & 6th items' ; call @show 5,2
call sy 'adding item before item 4: black' ; call @put 'black',4
say 'list size='@size()
call sy 'forward list'
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 @show
call sy 'backward list'
call sy 'showing list' ; call @show
call sy 'adding to head: Oy!' ; call @put 'Oy!',0
call @show ,,-1
call sy 'showing 4th item'
call sy 'showing list' ; call @show
exit /*stick a fork in it, 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)
p: return word(arg(1),1)
sy: say; say left('',30) "───" arg(1) '───'; return
@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. ---
─── initializing the list. ───


--- building list: Was it a cat I saw ---
─── building list: Was it a cat I saw ───


--- displaying list size. ---
─── displaying list size. ───
list size=6
list size=6


--- forward list ---
─── forward list ───
Was it a cat I saw
Was it a cat I saw


--- backward list ---
─── backward list ───
saw I cat a it Was
saw I cat a it Was


--- showing 4th item ---
─── showing 4th item ───
cat
cat


--- showing 6th & 6th items ---
─── showing 6th & 6th items ───
I saw
I saw


--- adding item before item 4: black ---
─── adding item before item 4: black ───


--- showing list ---
─── showing list ───
Was it a black cat I saw
Was it a black cat I saw


--- adding to tail: there, in the ... ---
─── adding to tail: there, in the ... ───


--- showing list ---
─── 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! ---
─── adding to head: Oy! ───


--- showing list ---
─── 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>