default.txt 953 B

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