In the comments, it turned out that this is a directed graph. If so, you could use something like this:
type
TNode = class
strict private
FName: string;
FOutgoingArcs: TList<TNode>;
function GetOutgoingArc(Index: Integer): TNode;
function GetOutgoingArcCount: Integer;
public
constructor Create(const AName: string);
procedure AddOutgoingArc(ANode: TNode);
procedure AddOutgoingArcs(const ANodes: array of TNode);
property OutgoingArcs[Index: Integer]: TNode read GetOutgoingArc;
property OutgoingArcCount: Integer read GetOutgoingArcCount;
property Name: string read FName write FName;
destructor Destroy; override;
end;
implementation
{ TNode }
procedure TNode.AddOutgoingArc(ANode: TNode);
begin
FOutgoingArcs.Add(ANode)
end;
procedure TNode.AddOutgoingArcs(const ANodes: array of TNode);
var
Node: TNode;
begin
for Node in ANodes do
AddOutgoingArc(Node);
end;
constructor TNode.Create(const AName: string);
begin
FName := AName;
FOutgoingArcs := TList<TNode>.Create;
end;
destructor TNode.Destroy;
begin
FOutgoingArcs.Free;
inherited;
end;
function TNode.GetOutgoingArcCount: Integer;
begin
Result := FOutgoingArcs.Count;
end;
function TNode.GetOutgoingArc(Index: Integer): TNode;
begin
Result := FOutgoingArcs[Index];
end;
I also think it is good to keep track of all nodes in a single list, so I'd do
var
Nodes: TObjectList<TNode>;
function CreateNode(const AName: string): TNode;
begin
Result := TNode.Create(AName);
Nodes.Add(Result);
end;
Now we can play (make sure to create Nodes first: Nodes := TObjectList<TNode>.Create(True{say})):
var
NewYork,
London,
Paris,
Moscow: TNode;
begin
NewYork := CreateNode('New York');
London := CreateNode('London');
Paris := CreateNode('Paris');
Moscow := CreateNode('Moscow');
NewYork.AddOutgoingArc(London);
London.AddOutgoingArcs([NewYork, Paris, Moscow]);
Paris.AddOutgoingArcs([London, Moscow]);
Moscow.AddOutgoingArc(NewYork);
But of course there are a thousand ways to design this. This is only one possible solution.
Update:
Notice that there is only a single object named "London", so if you change this "via Paris", it will be seen "via New York":
Paris.OutgoingArcs[0].Name := 'The Capital of the United Kingdom';
Now
NewYork.OutgoingArcs[0].Name
is also 'The Capital of the United Kingdom'.
Also notice that, with
Nodes := TObjectList<TNode>.Create(True)
the nodes will be owned by the Nodes object list (that's what True means), so they will be freed when Nodes is freed. So, for instance, if you use these nodes in your own class TTravelPlanner, you might want to create Nodes in TTravelPlanner.Create and do Nodes.Free in TTravelPlanner.Destroy.