Este codigo es de object pascal. (Free Pascal, Delphi)
Es una pila coordenadas.
Pos: es el dato para el cual se hizo la lista.
Next: es la liga.
Parrent: es el predecesor del punto en cuestion en un grafico. Tomalo como si fuera un dato mas que no importa mucho, al igual que Pos.
PopFirst: saca el primer elemento en la lista
Add: añade un elemento en la lista, existe 2 tipos de este procedimiento, uno añade el punto y el pariente, y el otro añade directamente por el nodo.
Exists: devuelve TRUE si la coordenada existe en la lista.
unit uPointList;
interface
uses
Types;
type
PPointNode = ^TPointNode;
TPointNode = record
Pos: TPoint;
Next: PPointNode;
Parrent: PPointNode;
end;
TPointList = class
private
FFirst: PPointNode;
FLast: PPointNode;
FCount: Integer;
function NewNode(ANext, AParrent: PPointNode; APoint: TPoint): PPointNode;
public
constructor Create;
destructor Destroy;
procedure Add(AParrent: PPointNode; APoint: TPoint); overload;
procedure Add(var APointNode: PPointNode); overload;
function ExistPoint(APoint: TPoint): Boolean;
function PopFirst: PPointNode;
property Count: Integer read FCount;
end;
implementation
uses
Dialogs;
{ TPointList }
procedure TPointList.Add(AParrent: PPointNode; APoint: TPoint);
var
Node: PPointNode;
begin
Node := NewNode(nil, Aparrent, APoint);
if (FFirst <> nil) then
FLast^.Next := Node
else
FFirst := Node;
FLast := Node;
Inc(FCount);
end;
procedure TPointList.Add(var APointNode: PPointNode);
begin
APointNode^.Next := nil;
if (FFirst <> nil) then
FLast^.Next := APointNode
else
FFirst := APointNode;
FLast := APointNode;
Inc(FCount);
end;
constructor TPointList.Create;
begin
inherited Create;
FFirst := nil;
FLast := nil;
FCount := 0;
end;
destructor TPointList.Destroy;
var
p,q: PPointNode;
begin
p := FFirst;
while p <> nil do
begin
q := p^.Next;
Dispose(p);
p := q;
end;
FCount := -1;
inherited Destroy;
end;
function TPointList.ExistPoint(APoint: TPoint): Boolean;
var
p,q: PPointNode;
begin
p := FFirst;
Result := False;
while p <> nil do
begin
q := p^.Next;
if (p^.Pos.X = APoint.X) and (p^.Pos.Y = APoint.Y) then Result := True;
p := q;
end;
end;
function TPointList.NewNode(ANext, AParrent: PPointNode; APoint: TPoint): PPointNode;
var
p: PPointNode;
begin
New(p);
p^.Pos := Apoint;
p^.Next := ANext;
p^.Parrent := AParrent;
Result := p;
end;
function TPointList.PopFirst: PPointNode;
begin
Result := FFirst;
FFirst := FFirst^.Next;
Dec(FCount);
end;
end.