make linked lists fully mutable

This commit is contained in:
Daniella 2024-10-14 05:02:28 +02:00
parent 2e126a6652
commit a8b0a1bb53
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
2 changed files with 44 additions and 30 deletions

View file

@ -12,16 +12,15 @@ construct LinkedList {
}
i
}
push { this | with item this ;
push { | with item this ;
item:unwrap;
this:value null eq if {
item this:=value;
this 2 stop
2 stop
}
LinkedList:new
dup :=value;<item>
this:last-entry:=next;
this
}
last-entry { list | with this ;
while { this:next null eq not } {
@ -47,17 +46,20 @@ construct LinkedList {
:=next;<null>
item:value
}
push-front { list | with item this ;
push-front { | with item this ;
item:unwrap;
this:value null eq if {
item this:=value;
this 2 stop
2 stop
}
LinkedList:new
dup :=next;<this>
dup :=value;<item>
"append new next identical to this, then make this contain new value";
def new LinkedList:new =new
this:next new:=next;
this:value new:=value;
item this:=value;
new this:=next;
}
insert { list | with item index this ;
insert { | with item index this ;
item:unwrap;
index 0 eq if {
item this:push-front 2 stop
@ -67,13 +69,19 @@ construct LinkedList {
list:next =list
index -- =index
}
item list:next:push-front list:=next;
this
item list:next:push-front
}
pop-front { item list | with this ;
this:value this:next
pop-front { item | with this ;
this:value
this:next null eq dup if {
null this:=value
}
not if {
this:next:value this:=value
this:next:next this:=next
}
}
remove { item list | with index this ;
remove { item | with index this ;
index 0 eq if {
this:pop-front 2 stop
}
@ -82,8 +90,7 @@ construct LinkedList {
list:next =list
index -- =index
}
list:next:pop-front list:=next
this
list:next:pop-front
}
get { item | with index this ;
while { index 0 gt } {
@ -92,6 +99,13 @@ construct LinkedList {
}
this:value
}
set { item | with value index this ;
while { index 0 gt } {
this:next =this
index -- =index
}
this:value value this:=value
}
foreach { | with callable this ;
while { this null eq not } {
this:value dup null eq not if { callable:call; }

View file

@ -1,11 +1,11 @@
[
"#stream.spl" import
"#http.spl" import
"#messaging.spl" import
"#server.spl" import
"#time.spl" import
"#httpserver/base.spl" import
"#linkedlist.spl" import
"spl/stream.spl" import
"spl/http.spl" import
"spl/messaging.spl" import
"spl/server.spl" import
"spl/time.spl" import
"spl/httpserver/base.spl" import
"spl/linkedlist.spl" import
"SPL tester" =program-name
@ -271,31 +271,31 @@ func main { int | with args ;
list:pop;
list:iter:join<", "> println;
"=> removing 5: " print
list:remove<5> =list pop
list:remove;<5>
list:iter:join<", "> println;
"=> popping front: " print
list:pop-front =list pop
list:pop-front;
list:iter:join<", "> println;
"=> inserting 0 back: " print
0 list:insert<0> =list
0 list:insert;<0>
list:iter:join<", "> println;
"=> inserting 5 back: " print
5 list:insert<5> =list
5 list:insert;<5>
list:iter:join<", "> println;
5 :foreach<{ | "" println }>
5 :foreach<{ | pop "" println }>
"you now have a chance to connect too: localhost :4075 :4076 - stopping in 5 seconds..." println;
5000 time:sleep;
] dup :len 0 eq not if {
"" println
"!! something went wrong somewhere. the stack is not empty." println
dyn-__dump
}
"you now have a chance to connect too: localhost :4075 :4076 - stopping in 5 seconds..." println;
5000 time:sleep;
100
}