Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library DelphiList;
- uses
- System.SysUtils,
- System.Classes;
- type
- TNode<T> = class
- Value: T;
- Next: TNode<T>;
- function ToString(): String;
- constructor CreateNew();
- end;
- TList<T> = class
- FirstNode: TNode<T>;
- Size: Integer;
- function ToString(): string;
- function GetAt(Index: Integer): T;
- function Equals(List: TList<T>; var DifferentFrom: Integer): Boolean;
- procedure Add(Value: T);
- procedure RemoveAt(Index: Integer);
- procedure Clear;
- constructor CreateNew();
- end;
- constructor TNode<T>.CreateNew();
- begin
- Next := nil;
- end;
- function TNode<T>.ToString(): String;
- begin
- if (TypeInfo(T) = TypeInfo(Integer)) then
- Result := IntToStr(PInteger(@Value)^);
- end;
- constructor TList<T>.CreateNew();
- begin
- Size := 0;
- FirstNode := nil;
- end;
- procedure TList<T>.Clear;
- begin
- self.FirstNode := nil;
- Size := 0;
- end;
- function TList<T>.Equals(List: TList<T>; var DifferentFrom: Integer): Boolean;
- var
- Node1, Node2: TNode<T>;
- var
- Size: Integer;
- begin
- DifferentFrom := 0;
- Result := True;
- Node1 := self.FirstNode;
- Node2 := List.FirstNode;
- if self.Size <> List.Size then
- Result := false;
- while (Node1 <> nil) and Result do
- begin
- if not CompareMem(@Node1.Value, @Node2.Value, SizeOf(Node1.Value)) then
- Result := false
- else
- begin
- inc(DifferentFrom);
- Node1 := Node1.Next;
- Node2 := Node2.Next;
- end;
- end;
- end;
- function TList<T>.GetAt(Index: Integer): T;
- var
- I: Integer;
- Node: TNode<T>;
- begin
- if (Size - 1 < Index) or (Index < 0) then
- raise EListError.Create('Index out of range');
- Node := FirstNode;
- for I := 1 to Index do
- Node := Node.Next;
- Result := Node.Value;
- end;
- procedure TList<T>.RemoveAt(Index: Integer);
- var
- I: Integer;
- Node: TNode<T>;
- begin
- if (Size - 1 < Index) or (Index < 0) then
- raise EListError.Create('Index out of range');
- Node := FirstNode;
- for I := 1 to Index - 1 do
- Node := Node.Next;
- if Index = 0 then
- FirstNode := FirstNode.Next
- else if Index < Size - 1 then
- Node.Next := Node.Next.Next
- else
- Node.Next := nil;
- dec(Size);
- end;
- procedure TList<T>.Add(Value: T);
- var
- NextNode: TNode<T>;
- NewNode: TNode<T>;
- begin
- if (self = nil) or (FirstNode = nil) then
- begin
- FirstNode := TNode<T>.Create;
- FirstNode.Value := Value;
- end
- else
- begin
- NextNode := FirstNode;
- while NextNode.Next <> nil do
- begin
- NextNode := NextNode.Next;
- end;
- NewNode := TNode<T>.Create;
- NewNode.Value := Value;
- NextNode.Next := NewNode;
- end;
- inc(Size);
- end;
- function TList<T>.ToString(): string;
- var
- NextNode: TNode<T>;
- Res: String;
- begin
- Res := '{';
- NextNode := FirstNode;
- while NextNode <> nil do
- begin
- Res := Res + NextNode.ToString + ' ';
- NextNode := NextNode.Next;
- end;
- Res := Res + '}';
- Result := Res;
- end;
- {$R *.res}
- begin
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement