Singly-linked list/Reversal: Difference between revisions
Content deleted Content added
Created page with "{{draft task}} :I don't even know how to reverse a linked-list, and I don't even know what that is. -- a YouTuber. Reverse a linked list. Obviously you can do it by turning it into a normal list and back, but feel free to use a smarter, possibly more efficient way." |
Added Algol 68 |
||
Line 4: | Line 4: | ||
Reverse a linked list. Obviously you can do it by turning it into a normal list and back, but feel free to use a smarter, possibly more efficient way. |
Reverse a linked list. Obviously you can do it by turning it into a normal list and back, but feel free to use a smarter, possibly more efficient way. |
||
=={{header|ALGOL 68}}== |
|||
Using the code from the [[Singly-linked list/Traversal#ALGOL_68]] Task |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN |
|||
MODE STRINGLIST = STRUCT(STRING value, REF STRINGLIST next); |
|||
# construct a STRINGLIST wuth a few elments # |
|||
STRINGLIST list := ("Big", |
|||
LOC STRINGLIST := ("fjords", |
|||
LOC STRINGLIST := ("vex", |
|||
LOC STRINGLIST := ("quick", |
|||
LOC STRINGLIST := ("waltz", |
|||
LOC STRINGLIST := ("nymph",NIL)))))); |
|||
# print the list and buuild the reverse list # |
|||
REF STRINGLIST node := list; |
|||
REF STRINGLIST reverse := REF STRINGLIST(NIL); |
|||
WHILE node ISNT REF STRINGLIST(NIL) DO |
|||
reverse := HEAP STRINGLIST |
|||
:= STRINGLIST( value OF node, reverse ); |
|||
print((value OF node, space)); |
|||
node := next OF node |
|||
OD; |
|||
print(newline); |
|||
# print the reverse list # |
|||
node := reverse; |
|||
WHILE node ISNT REF STRINGLIST(NIL) DO |
|||
print((value OF node, space)); |
|||
node := next OF node |
|||
OD; |
|||
print(newline) |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Big fjords vex quick waltz nymph |
|||
nymph waltz quick vex fjords Big |
|||
</pre> |
Revision as of 18:43, 1 April 2023
Singly-linked list/Reversal is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- I don't even know how to reverse a linked-list, and I don't even know what that is. -- a YouTuber.
Reverse a linked list. Obviously you can do it by turning it into a normal list and back, but feel free to use a smarter, possibly more efficient way.
ALGOL 68
Using the code from the Singly-linked list/Traversal#ALGOL_68 Task
BEGIN
MODE STRINGLIST = STRUCT(STRING value, REF STRINGLIST next);
# construct a STRINGLIST wuth a few elments #
STRINGLIST list := ("Big",
LOC STRINGLIST := ("fjords",
LOC STRINGLIST := ("vex",
LOC STRINGLIST := ("quick",
LOC STRINGLIST := ("waltz",
LOC STRINGLIST := ("nymph",NIL))))));
# print the list and buuild the reverse list #
REF STRINGLIST node := list;
REF STRINGLIST reverse := REF STRINGLIST(NIL);
WHILE node ISNT REF STRINGLIST(NIL) DO
reverse := HEAP STRINGLIST
:= STRINGLIST( value OF node, reverse );
print((value OF node, space));
node := next OF node
OD;
print(newline);
# print the reverse list #
node := reverse;
WHILE node ISNT REF STRINGLIST(NIL) DO
print((value OF node, space));
node := next OF node
OD;
print(newline)
END
- Output:
Big fjords vex quick waltz nymph nymph waltz quick vex fjords Big