tgeneric6.pp 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. {$mode objfpc}
  2. type
  3. generic TList<_T>=class(TObject)
  4. type
  5. PListItem = ^TListItem;
  6. TListItem = record
  7. data : _T;
  8. next : PListItem;
  9. end;
  10. var
  11. first,last : PListItem;
  12. procedure Add(item: _T);
  13. end;
  14. procedure TList.Add(item: _T);
  15. var
  16. newitem : PListItem;
  17. begin
  18. new(newitem);
  19. newitem^.data:=item;
  20. newitem^.next:=first;
  21. first:=newitem;
  22. end;
  23. type
  24. TMyIntList = specialize TList<integer>;
  25. TMyStringList = specialize TList<string>;
  26. var
  27. ilist : TMyIntList;
  28. slist : TMyStringList;
  29. someInt : integer;
  30. begin
  31. someInt:=10;
  32. ilist := TMyIntList.Create;
  33. ilist.Add(someInt);
  34. ilist.Add(someInt+1);
  35. writeln(ilist.first^.data);
  36. if ilist.first^.data<>11 then
  37. halt(1);
  38. writeln(ilist.first^.next^.data);
  39. if ilist.first^.next^.data<>10 then
  40. halt(1);
  41. slist := TMyStringList.Create;
  42. slist.Add('Test1');
  43. slist.Add('Test2');
  44. writeln(slist.first^.data);
  45. if slist.first^.data<>'Test2' then
  46. halt(1);
  47. writeln('ok');
  48. end.