1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
namespace LinkedList;
interface
uses
System.Text;
type
List<T> = public class
where T is Object;
private
method AppendToString(aBuilder: StringBuilder);
public
constructor(aData: T);
constructor(aData: T; aNext: List<T>);
property Next: List<T>;
property Data: T;
method ToString: string; override;
end;
implementation
constructor List<T>(aData: T);
begin
Data := aData;
end;
constructor List<T>(aData: T; aNext: List<T>);
begin
constructor(aData);
Next := aNext;
end;
method List<T>.ToString: string;
begin
with lBuilder := new StringBuilder do begin
AppendToString(lBuilder);
result := lBuilder.ToString();
end;
end;
method List<T>.AppendToString(aBuilder: StringBuilder);
begin
if assigned(Data) then
aBuilder.Append(Data.ToString)
else
aBuilder.Append('nil');
if assigned(Next) then begin
aBuilder.Append(', ');
Next.AppendToString(aBuilder);
end;
end;
end.
|