glinkedlisttest.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. program LLTest;
  2. {$apptype console}
  3. uses
  4. glinkedlist;
  5. type
  6. IMyIntf = interface
  7. function GetName: string;
  8. property Name: string read GetName;
  9. end;
  10. { TMyClass }
  11. TMyClass = class(TInterfacedObject, IMyIntf)
  12. protected
  13. FName: string;
  14. public
  15. constructor Create(const AName: string);
  16. function GetName: string;
  17. end;
  18. TIntfLL = specialize TLinkedList<IMyIntf>;
  19. { TTest }
  20. TTest = class
  21. FList: TIntfLL;
  22. procedure Notification(Sender: TObject; const Item: IMyIntf; Action: TCollectionNotification);
  23. procedure SetupItems;
  24. procedure PrintList;
  25. function Main: TTest;
  26. end;
  27. operator :=(const AValue: string): IMyIntf;
  28. begin
  29. Result := TMyClass.Create(AValue);
  30. end;
  31. { TTest }
  32. procedure TTest.Notification(Sender: TObject; const Item: IMyIntf;
  33. Action: TCollectionNotification);
  34. var
  35. LL: TIntfLL;
  36. begin
  37. LL := (Sender as TIntfLL);
  38. case Action of
  39. cnAdded:
  40. write('added');
  41. cnRemoved:
  42. write('removed');
  43. end;
  44. write(' "', Item.GetName, '"; ');
  45. write('count=', LL.Count, '; ');
  46. write('first=');
  47. if LL.First = nil then
  48. write('nil')
  49. else
  50. write('"' + LL.First^.Data.Name, '"');
  51. write(' ');
  52. write('last=');
  53. if LL.Last = nil then
  54. write('nil')
  55. else
  56. write('"' + LL.Last^.Data.Name, '" ');
  57. writeln;
  58. end;
  59. procedure TTest.SetupItems;
  60. begin
  61. // add items "1" to "8"
  62. FList.InsertLast('4')^.InsertAfter('5')^.InsertAfter('6');
  63. FList.InsertFirst('3')^.InsertBefore('2')^.InsertBefore('1');
  64. FList.Last^.InsertAfter('7')^.InsertAfter('8');
  65. end;
  66. procedure TTest.PrintList;
  67. var
  68. i: IMyIntf;
  69. begin
  70. write('"');
  71. for i in FList do
  72. write(i.GetName, ' ');
  73. writeln('"');
  74. end;
  75. function TTest.Main: TTest;
  76. var
  77. i: integer;
  78. item: TIntfLL.PItem;
  79. begin
  80. FList := TIntfLL.Create;
  81. try
  82. FList.OnNotify := @Notification;
  83. // setup and print items
  84. SetupItems;
  85. PrintList;
  86. WriteLn;
  87. // print ROL
  88. for i := 1 to 8 do
  89. begin
  90. FList.RotateLeft;
  91. PrintList;
  92. end;
  93. WriteLn;
  94. // print ROR
  95. for i := 1 to 8 do
  96. begin
  97. FList.RotateRight;
  98. PrintList;
  99. end;
  100. WriteLn;
  101. // print deleting first item
  102. for i := 1 to 8 do
  103. begin
  104. FList.Delete(FList.First);
  105. PrintList;
  106. end;
  107. WriteLn;
  108. // print deleting last item
  109. SetupItems;
  110. for i := 1 to 8 do
  111. begin
  112. FList.Delete(FList.Last);
  113. PrintList;
  114. end;
  115. WriteLn;
  116. // delete some item from middle
  117. SetupItems;
  118. PrintList;
  119. item := FList.First^.Next^.Next^.Next;
  120. WriteLn(item^.data.GetName);
  121. FList.Delete(item);
  122. PrintList;
  123. WriteLn;
  124. // clear all items
  125. FList.Clear;
  126. PrintList;
  127. WriteLn;
  128. finally
  129. FList.Free;
  130. end;
  131. Result:=Self;
  132. end;
  133. { TMyClass }
  134. constructor TMyClass.Create(const AName: string);
  135. begin
  136. inherited Create;
  137. FName := AName;
  138. end;
  139. function TMyClass.GetName: string;
  140. begin
  141. Result := FName;
  142. end;
  143. begin
  144. With TTest.Create do
  145. try
  146. Main;
  147. finally
  148. Free;
  149. end;
  150. end.