Singly-linked list/Element removal: Difference between revisions

Added solution for Action!
(Added solution for Action!)
Line 10:
<br><br>
 
 
=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<lang Action!>CARD EndProg ;required for ALLOCATE.ACT
 
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
 
DEFINE PTR="CARD"
DEFINE NODE_SIZE="4"
TYPE ListNode=[PTR data,nxt]
 
ListNode POINTER listBegin
 
PTR FUNC FindLast()
ListNode POINTER last
last=listBegin
IF last=0 THEN
RETURN (0)
FI
WHILE last.nxt#0
DO
last=last.nxt
OD
RETURN (last)
 
PTR FUNC FindPrev(ListNode POINTER n)
ListNode POINTER prev
 
IF n=0 OR n=listBegin THEN
prev=0
ELSE
prev=listBegin
WHILE prev#0 AND prev.nxt#n
DO
prev=prev.nxt
OD
FI
RETURN (prev)
PROC Append(CHAR ARRAY v)
ListNode POINTER n,last
 
n=Alloc(NODE_SIZE)
n.data=v
n.nxt=0
last=FindLast()
IF last THEN
last.nxt=n
ELSE
listBegin=n
FI
RETURN
 
PROC Remove(ListNode POINTER n)
ListNode POINTER prev,next
IF n=0 THEN Break() FI
 
prev=FindPrev(n)
next=n.nxt
IF prev THEN
prev.nxt=next
ELSE
listBegin=next
FI
 
Free(n,NODE_SIZE)
RETURN
 
PROC PrintList()
ListNode POINTER n
 
n=listBegin
Print("(")
WHILE n
DO
Print(n.data)
IF n.nxt THEN
Print(", ")
FI
n=n.nxt
OD
PrintE(")") PutE()
RETURN
 
PROC TestRemove(ListNode POINTER n)
PrintF("Remove ""%S"":%E",n.data)
Remove(n)
PrintList()
RETURN
 
PROC Main()
ListNode POINTER p
Put(125) PutE() ;clear screen
AllocInit(0)
listBegin=0
 
Append("First")
Append("Second")
Append("Third")
Append("Fourth")
Append("Fifth")
PrintList()
 
TestRemove(listBegin.nxt)
p=FindLast()
p=FindPrev(p)
TestRemove(p)
TestRemove(listBegin)
p=FindLast()
TestRemove(p)
TestRemove(listBegin)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Singly-linked_list_element_removal.png Screenshot from Atari 8-bit computer]
<pre>
(First, Second, Third, Fourth, Fifth)
 
Remove "Second":
(First, Third, Fourth, Fifth)
 
Remove "Fourth":
(First, Third, Fifth)
 
Remove "First":
(Third, Fifth)
 
Remove "Fifth":
(Third)
 
Remove "Third":
()
</pre>
 
=={{header|ALGOL 68}}==
Anonymous user