Doubly-linked list/Traversal: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring the hard way) |
(Added solution for Action!) |
||
Line 7: | Line 7: | ||
{{Template:See also lists}} |
{{Template:See also lists}} |
||
<br><br> |
<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 NODE_SIZE="6" |
|||
TYPE ListNode=[INT data CARD prv,nxt] |
|||
ListNode POINTER listBegin,listEnd |
|||
PROC Append(INT 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 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 ForwardTraverse() |
|||
ListNode POINTER n |
|||
n=listBegin |
|||
PrintE("Forward traverse:") |
|||
Print("(") |
|||
WHILE n |
|||
DO |
|||
PrintI(n.data) |
|||
IF n.nxt THEN |
|||
Print(", ") |
|||
FI |
|||
n=n.nxt |
|||
OD |
|||
PrintE(")") |
|||
RETURN |
|||
PROC BackwardTraverse() |
|||
ListNode POINTER n |
|||
n=listEnd |
|||
PrintE("Backward traverse") |
|||
Print("(") |
|||
WHILE n |
|||
DO |
|||
PrintI(n.data) |
|||
IF n.prv THEN |
|||
Print(", ") |
|||
FI |
|||
n=n.prv |
|||
OD |
|||
PrintE(")") |
|||
RETURN |
|||
PROC Main() |
|||
INT i |
|||
Put(125) PutE() ;clear screen |
|||
AllocInit(0) |
|||
listBegin=0 |
|||
listEnd=0 |
|||
FOR i=0 TO 50 |
|||
DO |
|||
Append(i*i) |
|||
OD |
|||
ForwardTraverse() |
|||
PutE() |
|||
BackwardTraverse() |
|||
Clear() |
|||
RETURN</lang> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Doubly-linked_list_traversal.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Forward traverse: |
|||
(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, |
|||
400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, |
|||
1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500) |
|||
Backward traverse |
|||
(2500, 2401, 2304, 2209, 2116, 2025, 1936, 1849, 1764, 1681, 1600, 1521, 1444, 1369, 1296, |
|||
1225, 1156, 1089, 1024, 961, 900, 841, 784, 729, 676, 625, 576, 529, 484, 441, 400, 361, |
|||
324, 289, 256, 225, 196, 169, 144, 121, 100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0) |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |