Doubly-linked list/Element insertion: Difference between revisions
Content added Content deleted
(Add swift) |
(Added solution for Action!) |
||
Line 4: | Line 4: | ||
{{Template:See also lists}} |
{{Template:See also lists}} |
||
=={{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 NODE_SIZE="5" |
|||
TYPE ListNode=[CHAR data CARD prv,nxt] |
|||
ListNode POINTER listBegin,listEnd |
|||
PROC AddBegin(CHAR v) |
|||
ListNode POINTER n |
|||
n=Alloc(NODE_SIZE) |
|||
n.data=v |
|||
n.prv=0 |
|||
n.nxt=listBegin |
|||
IF listBegin THEN |
|||
listBegin.prv=n |
|||
ELSE |
|||
listEnd=n |
|||
FI |
|||
listBegin=n |
|||
RETURN |
|||
PROC AddEnd(CHAR v) |
|||
ListNode POINTER n |
|||
n=Alloc(NODE_SIZE) |
|||
n.data=v |
|||
n.prv=listEnd |
|||
n.nxt=0 |
|||
IF listEnd THEN |
|||
listEnd.nxt=n |
|||
ELSE |
|||
listBegin=n |
|||
FI |
|||
listEnd=n |
|||
RETURN |
|||
PROC AddAfter(CHAR v ListNode POINTER node) |
|||
ListNode POINTER n,tmp |
|||
IF node=0 THEN |
|||
PrintE("The node is null!") Break() |
|||
ELSEIF node=listEnd THEN |
|||
AddEnd(v) |
|||
ELSE |
|||
n=Alloc(NODE_SIZE) |
|||
n.data=v |
|||
n.nxt=node.nxt |
|||
n.prv=node |
|||
tmp=node.nxt |
|||
tmp.prv=n |
|||
node.nxt=n |
|||
FI |
|||
RETURN |
|||
PROC Clear() |
|||
ListNode POINTER n,next |
|||
n=listBegin |
|||
WHILE n |
|||
DO |
|||
next=n.nxt |
|||
Free(n,NODE_SIZE) |
|||
n=next |
|||
OD |
|||
listBegin=0 |
|||
listEnd=0 |
|||
RETURN |
|||
PROC PrintList() |
|||
ListNode POINTER n |
|||
n=listBegin |
|||
Print("(") |
|||
WHILE n |
|||
DO |
|||
Put(n.data) |
|||
IF n.nxt THEN |
|||
Print(", ") |
|||
FI |
|||
n=n.nxt |
|||
OD |
|||
PrintE(")") |
|||
RETURN |
|||
PROC TestAddBegin(CHAR v) |
|||
AddBegin(v) |
|||
PrintF("Add '%C' at the begin:%E",v) |
|||
PrintList() |
|||
RETURN |
|||
PROC TestAddAfter(CHAR v ListNode POINTER node) |
|||
AddAfter(v,node) |
|||
PrintF("Add '%C' after '%C':%E",v,node.data) |
|||
PrintList() |
|||
RETURN |
|||
PROC TestClear() |
|||
Clear() |
|||
PrintE("Clear the list:") |
|||
PrintList() |
|||
RETURN |
|||
PROC Main() |
|||
Put(125) PutE() ;clear screen |
|||
AllocInit(0) |
|||
listBegin=0 |
|||
listEnd=0 |
|||
PrintList() |
|||
TestAddBegin('A) |
|||
TestAddAfter('B,listBegin) |
|||
TestAddAfter('C,listBegin) |
|||
TestClear() |
|||
RETURN</lang> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Doubly-linked_list_element_insertion.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
() |
|||
Add 'A' at the begin: |
|||
(A) |
|||
Add 'B' after 'A': |
|||
(A, B) |
|||
Add 'C' after 'A': |
|||
(A, C, B) |
|||
Clear the list: |
|||
() |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |