Singly-linked list/Element insertion: Difference between revisions
Singly-linked list/Element insertion (view source)
Revision as of 11:41, 6 February 2024
, 3 months ago→{{header|Wren}}: Minor tidy
(→{{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 = .
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
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
Call set_list 3 /* linked list: 12 Proth primes */
Call set_list 5 /*see https://mathworld.wolfram.com/ProthPrime.html*/
Call set_list 13
Call set_list 17
Call set_list 41
Call set_list 97
Call set_list 113
Call set_list 193
Call set_list 241
Call set_list 257
Call set_list 353
Call set_list 449
Call show_list
nnn=z..after
Call ins_list nnn,newval /* perform the insertion */
Say ''
Say 'a new value of' newval 'has been inserted',
Call show_list
Exit /* stick a fork in it, we're done.*/
set_list: Procedure Expose z.
last=z.0 /* set the previous last element. */
new=z.0+1 /* set the new ast element. */
z.new.0next=0 /* set the next pointer value. */
z..value=new /* set a locator pointer to self. */
z.0width=max(z.0width,length(value)) /*set maximum width of any value*/
Return
ins_list: Procedure Expose z.
Parse Arg nnn,value
z.0=z.0+1 /* bump number of list elements. */
last=z.0 /* position of the new value */
z.last.0value=value /* store the new value */
z.last.0next=z.nnn.0next /* uptate the pointers to the */
z.0width=max(z.0width,length(value)) /*set maximum width of any value*/
Return
show_list:
Say
Say center('item',6) 'position' center('value',w) center('next',6)
Say center('',6,'-') '--------' center('',w,'-') center('',6,'-')
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>
=={{header|Ruby}}==
Line 2,117 ⟶ 2,160:
=={{header|Wren}}==
{{libheader|Wren-llist}}
<syntaxhighlight lang="
var ll = LinkedList.new(["A", "B"])
|