Doubly-linked list/Definition: Difference between revisions

m
(→‎{{header|ALGOL 68}}: Preoperative preparation and premedication)
Line 18:
{{works with|ALGOL 68|Revision 1 - one extension to language used - PRAGMA READ - a non standard feature similar to C's #include directive.}}{{works with|ALGOL 68G|Any - tested with release algol68g-2.7}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
'''''File: prelude/Doubly-linked_list_Element.a68'''<lang algol68># -*- coding: utf-8 -*- #
MODE MNODENODENEW = STRUCT (
NODE pred,
succ,
DATA value
);
 
MODE NODE = REF MNODENODENEW;
 
SKIP</lang>'''File: prelude/Doubly-linked_list_Class.a68'''<lang algol68># -*- coding: utf-8 -*- #
Line 37 ⟶ 39:
PROC (LIST, NODE, NODE)NODE insert after,
PROC (LIST, NODE)NODE remove node
) class list;
 
SKIP</lang>'''File: prelude/Doubly-linked_list_Definition.a68'''<lang algol68># -*- coding: utf-8 -*- #
MODE LIST = REF MNODENODENEW;
MODE NODE = REF MNODE;
 
new list OF class list := LIST: (
HEAP MNODENODENEW master link;
master link := (master link, master link, ~);
master link
);
 
is empty OF class list := (LIST self)BOOL:
(LIST(pred OF self) :=: LIST(self)) AND (LIST(self) :=: LIST(succ OF self));
 
get head OF class list := (LIST self)NODE:
succ OF self;
 
get tail OF class list := (LIST self)NODE:
pred OF self;
 
add tail OF class list := (LIST self, NODE node)NODE:
(insert after OF class list)(self, pred OF self, node);
 
add head OF class list := (LIST self, NODE node)NODE:
(insert after OF class list)(self, succ OF self, node);
 
remove head OF class list := (LIST self)NODE:
(remove node OF class list)(self, succ OF self);
 
remove tail OF class list := (LIST self)NODE:
(remove node OF class list)(self, pred OF self);
 
insert after OF class list := (LIST self, NODE cursor, NODE node)NODE: (
succ OF node := succ OF cursor;
pred OF node := cursor;
Line 78 ⟶ 79:
);
 
remove node OF class list := (LIST self, NODE node)NODE: (
succ OF pred OF node := succ OF node;
pred OF succ OF node := pred OF node;
Line 96 ⟶ 97:
[]DATA sample = ("Was", "it", "a", "cat", "I", "saw");
 
LIST list a := new list OF class list;
 
NODE tmp;
Line 103 ⟶ 104:
# Add some data to a list #
FOR i TO UPB sample DO
tmp := HEAP MNODENODENEW;
IF tmp :/=: NODE(NIL) THEN # technically "tmp" is never NIL #
value OF tmp := sample[i];
(add tail OF class list)(list a, tmp)
FI
OD;
 
# Iterate throught the list forward #
NODE node := (get head OF class list)(list a);
print("Iterate forward: ");
WHILE node :/=: NODE(list a) DO
Line 120 ⟶ 121:
 
# Iterate throught the list backward #
node := (get tail OF class list)(list a);
print("Iterate backward: ");
WHILE node :/=: NODE(list a) DO
Line 130 ⟶ 131:
# Finally empty the list #
print("Empty from tail: ");
WHILE NOT (is empty OF class list)(list a) DO
tmp := (remove tail OF class list)(list a);
print((value OF tmp, " "))
# sweep heap #