lists.pp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. { List Operations }
  2. Program lists;
  3. uses SysUtils, classes;
  4. const SIZE : longint = 10000;
  5. Function test_lists : integer;
  6. var
  7. i, len1, len2 : longint;
  8. Li1, Li2, Li3 : TList;
  9. lists_equal : Integer;
  10. begin
  11. Li1 := TList.Create;
  12. Li1.Capacity := SIZE;
  13. For i := 0 to SIZE Do
  14. Li1.Add(Pointer(i));
  15. Li2 := TList.Create;
  16. Li2.Capacity := SIZE;
  17. For i:= 0 to SIZE Do
  18. Li2.Add(Li1.Items[i]);
  19. { remove each individual item from left side of Li2 and
  20. append to right side of Li3 (preserving order) }
  21. Li3 := TList.Create;
  22. Li3.Capacity := SIZE;
  23. For i := 0 to SIZE Do
  24. begin
  25. Li3.Add( Li2.First );
  26. Li2.Remove( Li2.First );
  27. end;
  28. { remove each individual item from right side of Li3 and
  29. append to right side of Li2 (reversing list) }
  30. For i := 0 To SIZE Do
  31. begin
  32. Li2.Add( Li3.Last );
  33. Li3.Count -= 1;
  34. end;
  35. For i := 0 To (SIZE div 2) Do
  36. begin
  37. Li1.Exchange( i, SIZE-i );
  38. end;
  39. If longint(Li1.first) <> SIZE Then
  40. begin
  41. test_lists := 0;
  42. exit;
  43. end;
  44. len1 := Li1.Count - 1;
  45. len2 := Li2.Count - 1;
  46. If len1 <> len2 Then
  47. begin
  48. test_lists := 0;
  49. exit;
  50. end;
  51. lists_equal := 1;
  52. For i := 0 To len1 Do
  53. begin
  54. If longint(Li1.items[i]) <> longint(Li2.items[i]) Then
  55. begin
  56. lists_equal := 0;
  57. break;
  58. end;
  59. end;
  60. If lists_equal = 0 Then
  61. begin
  62. test_lists := 0;
  63. end
  64. else
  65. test_lists := len1;
  66. end;
  67. var
  68. ITER, i, result: integer;
  69. begin
  70. if ParamCount = 0 then
  71. ITER := 1
  72. else
  73. ITER := StrToInt(ParamStr(1));
  74. if ITER < 1 then ITER := 1;
  75. For i := 1 To ITER Do result := test_lists();
  76. Writeln (IntToStr(result));
  77. end.