Advertisement
deced

Untitled

Mar 7th, 2021
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.25 KB | None | 0 0
  1. library DelphiList;
  2.  
  3. uses
  4.     System.SysUtils,
  5.     System.Classes;
  6.  
  7. type
  8.     TNode<T> = class
  9.         Value: T;
  10.         Next: TNode<T>;
  11.         function ToString(): String;
  12.         constructor CreateNew();
  13.     end;
  14.  
  15.     TList<T> = class
  16.         FirstNode: TNode<T>;
  17.         Size: Integer;
  18.         function ToString(): string;
  19.         function GetAt(Index: Integer): T;
  20.         function Equals(List: TList<T>; var DifferentFrom: Integer): Boolean;
  21.         procedure Add(Value: T);
  22.         procedure RemoveAt(Index: Integer);
  23.         procedure Clear;
  24.  
  25.         constructor CreateNew();
  26.     end;
  27.  
  28. constructor TNode<T>.CreateNew();
  29. begin
  30.     Next := nil;
  31. end;
  32.  
  33. function TNode<T>.ToString(): String;
  34. begin
  35.     if (TypeInfo(T) = TypeInfo(Integer)) then
  36.         Result := IntToStr(PInteger(@Value)^);
  37. end;
  38.  
  39. constructor TList<T>.CreateNew();
  40. begin
  41.     Size := 0;
  42.     FirstNode := nil;
  43. end;
  44.  
  45. procedure TList<T>.Clear;
  46. begin
  47.     self.FirstNode := nil;
  48.     Size := 0;
  49. end;
  50.  
  51. function TList<T>.Equals(List: TList<T>; var DifferentFrom: Integer): Boolean;
  52. var
  53.     Node1, Node2: TNode<T>;
  54. var
  55.     Size: Integer;
  56. begin
  57.     DifferentFrom := 0;
  58.     Result := True;
  59.     Node1 := self.FirstNode;
  60.     Node2 := List.FirstNode;
  61.     if self.Size <> List.Size then
  62.         Result := false;
  63.     while (Node1 <> nil) and Result do
  64.     begin
  65.         if not CompareMem(@Node1.Value, @Node2.Value, SizeOf(Node1.Value)) then
  66.             Result := false
  67.         else
  68.         begin
  69.             inc(DifferentFrom);
  70.             Node1 := Node1.Next;
  71.             Node2 := Node2.Next;
  72.         end;
  73.     end;
  74. end;
  75.  
  76. function TList<T>.GetAt(Index: Integer): T;
  77. var
  78.     I: Integer;
  79.     Node: TNode<T>;
  80. begin
  81.     if (Size - 1 < Index) or (Index < 0) then
  82.         raise EListError.Create('Index out of range');
  83.     Node := FirstNode;
  84.     for I := 1 to Index do
  85.         Node := Node.Next;
  86.     Result := Node.Value;
  87. end;
  88.  
  89. procedure TList<T>.RemoveAt(Index: Integer);
  90. var
  91.     I: Integer;
  92.     Node: TNode<T>;
  93. begin
  94.     if (Size - 1 < Index) or (Index < 0) then
  95.         raise EListError.Create('Index out of range');
  96.     Node := FirstNode;
  97.     for I := 1 to Index - 1 do
  98.         Node := Node.Next;
  99.     if Index = 0 then
  100.         FirstNode := FirstNode.Next
  101.     else if Index < Size - 1 then
  102.         Node.Next := Node.Next.Next
  103.     else
  104.         Node.Next := nil;
  105.     dec(Size);
  106.  
  107. end;
  108.  
  109. procedure TList<T>.Add(Value: T);
  110. var
  111.     NextNode: TNode<T>;
  112.     NewNode: TNode<T>;
  113. begin
  114.     if (self = nil) or (FirstNode = nil) then
  115.     begin
  116.         FirstNode := TNode<T>.Create;
  117.         FirstNode.Value := Value;
  118.     end
  119.     else
  120.     begin
  121.  
  122.         NextNode := FirstNode;
  123.         while NextNode.Next <> nil do
  124.         begin
  125.  
  126.             NextNode := NextNode.Next;
  127.         end;
  128.         NewNode := TNode<T>.Create;
  129.         NewNode.Value := Value;
  130.         NextNode.Next := NewNode;
  131.     end;
  132.     inc(Size);
  133. end;
  134.  
  135. function TList<T>.ToString(): string;
  136. var
  137.     NextNode: TNode<T>;
  138.     Res: String;
  139. begin
  140.     Res := '{';
  141.     NextNode := FirstNode;
  142.     while NextNode <> nil do
  143.     begin
  144.         Res := Res + NextNode.ToString + ' ';
  145.         NextNode := NextNode.Next;
  146.     end;
  147.     Res := Res + '}';
  148.     Result := Res;
  149. end;
  150. {$R *.res}
  151.  
  152. begin
  153.  
  154. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement