Singly-linked list/Element insertion: Difference between revisions

m
(→‎{{header|jq}}: def insert($x):)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 2 users not shown)
Line 1,539:
<syntaxhighlight lang="mathematica">Append[{a, b}, c]
->{a, b, c}</syntaxhighlight>
 
Node = {"item": null, "next": null}
Node.init = function(item)
node = new Node
node.item = item
return node
end function
 
=={{header|MiniScript}}==
We're choosing here to use the built-in list type, rather than make our own from scratch, since this is more representative of how one is likely to actually use MiniScript.
<syntaxhighlight lang="miniscript">
> myList = [100, 101, 102]
> myList.push 103
[100, 101, 102, 103]
> myList.insert 0, 99
[99, 100, 101, 102, 103]
> myList.insert 3,101.5
[99, 100, 101, 101.5, 102, 103]
</syntaxhighlight>
 
=={{header|Modula-3}}==
Line 1,654 ⟶ 1,673:
See [[Singly-linked_list/Element_definition#ooRexx|Single-linked list/Element definition]] for full class definition.
<syntaxhighlight lang="oorexx">
list = .linkedlistlist~new
index = list~insert("abc") -- insert a first item, keeping the index
Call show
list~insert("def") -- adds to the end
Call show
list~insert("123", .nil) -- adds to the begining
Call show
list~insert("456", index) -- inserts between "abc" and "def"
Call show
list~remove(index) -- removes "abc"
Call show
</syntaxhighlight>
exit
show:
s=''
Do x over list
s=s x
end
say s
Return</syntaxhighlight>
{{out]]
<pre> abc
abc def
123 abc def
123 abc 456 def
123 456 def
</pre>
 
=={{header|Pascal}}==
Line 1,913 ⟶ 1,951:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program demonstrates how to create and show a single-linked list. */
@.=0/* and how to insert an element /*define a null linked list. */
call set@ 3z.=0 /*linked list:define a 12null Prothlinked primesz. */
Call set_list 3 /* linked list: 12 Proth primes */
call set@ 5
Call set_list 5 /*see https://mathworld.wolfram.com/ProthPrime.html*/
call set@ 13
Call set_list 13
call set@ 17
Call set_list 17
call set@ 41
Call set_list 41
call set@ 97
Call set_list 97
call set@ 113
Call set_list 113
call set@ 193
Call set_list 193
call set@ 241
Call set_list 241
call set@ 257
Call set_list 257
call set@ 353
Call set_list 353
call set@ 449
Call set_list 449
call list@
Call show_list
after = 97 /* ◄──── REXX code to do insert. */
newValnewval=100 /* ◄──── Insert this "value " " " " */
#after=@..after 97 /* ◄──── " " " " after the "element with this value */
nnn=z..after call ins@ #,newVal /* ◄──── " "position of z´this "value " " */
Call ins_list nnn,newval /* perform the insertion */
say
Say ''
say 'a new value of' newval "has been inserted after element value:" after
Say 'a new value of' newval 'has been inserted',
call list@
exit 'after element having the /*stick a fork in it, wevalue:'re done.*/after
Call show_list
/*──────────────────────────────────INS@ subroutine─────────────────────*/
Exit /* stick a fork in it, we're done.*/
ins@: procedure expose @.; parse arg #,y
 
@._last=@._last+1 /*bump number of list elements. */
set_list: Procedure Expose z.
_=@._last
@._._value=y Parse Arg value /* get element /*defineto newbe valueadded to list element. */
last=z.0 /* set the previous last element. */
@._._next=@.#._next
new=z.0+1 /* set the new ast element. */
@.#._next=_
@. z.y0=_ new /*set adefine locatornext pointeritem toin self.linked list*/
@ z.max_width=max(@last.max_width,length(y))0next=new /* set maximumthe width ofnext any pointer value. */
return z.new.0value=value /*return toset invokeritem ofto thisthe sub.value specified*/
z.new.0next=0 /* set the next pointer value. */
/*──────────────────────────────────LIST@ subroutine────────────────────*/
z..value=new /* set a locator pointer to self. */
list@: say; w=max(7, @.max_width ) /*use the max width of nums or 7.*/
z.0width=max(z.0width,length(value)) /*set maximum width of any value*/
say center('item',6) center('value',w) center('next',6)
Return
say center('' ,6,'─') center('' ,w,'─') center('' ,6,'─')
 
p=1
ins_list: Procedure Expose z.
do j=1 until p==0 /*show all entries of linked list*/
Parse Arg nnn,value
say right(j,6) right(@.p._value,w) right(@.p._next,6)
z.0=z.0+1 /* bump number of list elements. */
p=@.p._next
last=z.0 /* position of the new value */
end /*j*/
z.last.0value=value /* store the new value */
return
z.last.0next=z.nnn.0next /* uptate the pointers to the */
/*──────────────────────────────────SET@ subroutine─────────────────────*/
set@: procedure expose @z.;nnn.0next=last parse arg y /*get next element to be added to list */
_=@._last z..value=last /* store position /*setof the previous last element.new value*/
z.0width=max(z.0width,length(value)) /*set maximum width of any value*/
n=_+1 /*bump last ptr in linked list. */
Return
@._._next=n /*set the next pointer value. */
 
@._last=n /*define next item in linked list*/
show_list:
@.n._value=y /*set item to the value specified*/
Say
@.n._next=0 /*set the next pointer value. */
@..y=n w=max(7,z.0width) /* use the max width of nums or /*set a locator pointer to self7. */
Say center('item',6) 'position' center('value',w) center('next',6)
@.max_width=max(@.max_width,length(y)) /*set maximum width of any value.*/
Say center('',6,'-') '--------' center('',w,'-') center('',6,'-')
return /*return to invoker of this sub. */</syntaxhighlight>
p=1
Do j=1 Until p==0 /* show all entries of linked list*/
Say right(j,6) right(p,8) right(z.p.0value,w) right(z.p.0next,6)
p=z.p.0next
End /* j */
Return</syntaxhighlight>
'''output'''
<pre>
item position value next
------ -------- ------- ------
────── ─────── ──────
1 1 3 2
2 2 5 3
3 3 13 4
4 4 17 5
5 5 41 6
6 6 97 7
7 7 113 8
8 8 193 9
9 9 241 10
10 10 257 11
11 11 353 12
12 12 449 0
 
a new value of 100 has been inserted after element having the value: 97
 
item position value next
------ -------- ------- ------
────── ─────── ──────
1 1 3 2
2 2 5 3
3 3 13 4
4 4 17 5
5 5 41 6
6 6 97 13
7 13 100 7
8 7 113 8
9 8 193 9
10 9 241 10
11 10 257 11
12 11 353 12
13 12 449 0</pre>
</pre>
 
=={{header|Ruby}}==
Line 2,117 ⟶ 2,160:
=={{header|Wren}}==
{{libheader|Wren-llist}}
<syntaxhighlight lang="ecmascriptwren">import "./llist" for LinkedList
 
var ll = LinkedList.new(["A", "B"])
9,476

edits