Anonymous user
Doubly-linked list/Definition: Difference between revisions
→{{header|ALGOL 68}}: now using operators
(→{{header|ALGOL 68}}: now using operators) |
|||
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}}
COMMENT REQUIRES:
MODE VALUE = ~;
# For example: #
MODE VALUE = UNION(INT, REAL, COMPL)
END COMMENT
MODE LINKNEW = STRUCT (
LINK next, prev,
VALUE value
);
MODE
SKIP</lang>'''File: prelude/Doubly-
MODE LISTNEW = LINKNEW;
MODE LIST = REF LISTNEW;
OP LISTINIT = (LIST self)LIST: (
self := (self, self, ~);
self
);
(LIST(
# insert after #
OP +:= = (LINK cursor, LINK link)LINK: (
next OF link := next OF cursor;
prev OF link := cursor;
next OF cursor := link;
prev OF next OF link := link;
link
);
# insert before #
OP +=: = (
# delete current and step forward #
OP -:= = (LIST ignore, LINK link)LINK: (
next OF prev OF link := next OF link;
next OF link := prev OF link := NIL; # garbage collection hint #
link
);
# delete current and step backward #
PRIO -=: = 1;
OP -=: = (LIST link, LIST ignore)LINK: (
ignore -:= link; prev OF link
);
PRIO ISIN = 1; # low priority #
OP ISIN = (LINK link, LIST self)BOOL:
link ISNT LINK(self);
SKIP</lang>'''File: test/Doubly-linked_list_Operator_Usage.a68'''<lang algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
MODE VALUE = STRING; # user defined data type #
PR READ "prelude/Doubly-
PR READ "prelude/Doubly-
main: (
[]
LIST example list := LISTINIT HEAP LISTNEW;
LINK this;
# Add some data to a list #
value OF this := sample[i];
TAIL example list
OD;
# Iterate throught the list forward #
# Iterate throught the list backward #
# Finally empty the list #
this := (example list
OD;
print(new
)</lang>'''Output:'''
<pre>
|