Integers are read and stored in a linked list. The input stops when the number 0 is entered, and the stored integers are printed in the following format: 1 → 2 → …
The code is:
program ZeigerEinlesenEcho (input, output);
type
PointerElement = ^Element;
Element = record
Data : integer;
Next : PointerElement
end;
var
Start, Last, Current : PointerElement;
Zahl, Counter : integer;
begin
new (Start);
Current := Start;
writeln('Dieses Programm erstellt eine Liste ganzer Zahlen.');
writeln('Gib eine ganze Zahl ein (Die Zahl 0 bricht die Eingabe ab): ');
readln(Zahl);
while Zahl <> 0 do
begin
new (Last);
Current^.Data := Zahl;
Current^.Next := Last;
Current := Last;
readln(Zahl);
end;
Current^.Next := nil;
Current := Start;
Counter := 0;
while Current <> nil do
begin
Counter := Counter + 1;
write(Counter, '. ',Current^.Data,' --> ' );
Current := Current^.Next;
end;
writeln();
write('Die letzte Eingabe war: ', Zahl, Current^.Data);
end.
I expected the output to be the integers that I entered: 1 → a → 2 → b → … where a and b are the read integers.
The entered integers are printed correctly, but additional unexpected integers and an error code appear at the end.
The output is:
Free Pascal Compiler version 3.2.0 [2022/02/07] for x86_64
Copyright (c) 1993-2020 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling main.p
Linking main
43 lines compiled, 0.7 sec
Dieses Programm erstellt eine Liste ganzer Zahlen.
Gib eine ganze Zahl ein (Die Zahl 0 bricht die Eingabe ab):
1
2
3
4
0
1. 1 --> 2. 2 --> 3. 3 --> 4. 4 --> 5. -16168 -->
Runtime error 216 at $000000000040135D
$000000000040135D
$0000000000422FDC
Die letzte Eingabe war: 0