Thema: Delphi Die verkettete Liste

Einzelnen Beitrag anzeigen

Koller

Registriert seit: 22. Mär 2005
59 Beiträge
 
#7

Re: Die verkettete Liste

  Alt 18. Jan 2007, 20:56
Jetzt habe ich das nächste Problem:
Nun ist die Methode Clear dran. Erstmal die ganze Unit nochmal:
Delphi-Quellcode:
unit ParameterList;

interface

uses Parameter;

type
  PItem = ^TItem;
  TItem = record
    Data: TParameter;
    Next: PItem;
  end;

  TParameterlist = class
    private
      FFirst: PItem;
      FCurrently: PItem;

      FCount: integer;
      
      function GetParameter(index: integer): TParameter;
      procedure SetParameter(index: integer; const Value: TParameter);
      procedure SetPointerAtPosition(index: integer);
    
    public
      property Parameter[index: integer]: TParameter read GetParameter write SetParameter;
      property Count: integer read FCount;

      procedure AddAtFirst(Name, DataType: string);
      procedure AddAtPos(Name, DataType: string);
      procedure AddAtEnd(Name, DataType: string);
      procedure Delete(index: integer);
      procedure Swap(index1, index2: integer);
      procedure Clear;
      function High: integer;
      constructor Create;
      destructor Destroy;
  end;



implementation

{ TParameterlist }

procedure TParameterlist.SetPointerAtPosition(index: integer);
var i: integer;
begin
  if (index <= High) then
  begin
    FCurrently := FFirst;
    i := 0;
    while (i < index) do
    begin
     FCurrently := FCurrently.Next; //<-- AV
      inc(i);
    end;
  end;
end;


procedure TParameterlist.AddAtEnd(Name, DataType: string);
var param: TParameter; NewItem: PItem;
begin
  param := TParameter.Create(Name,DataType);
 //If list is still empty
  if (FFirst = nil) then
  begin
    New(NewItem);
    NewItem.Data := param;
    FFirst := NewItem;
    FCurrently := FFirst;
  end
  //list just has content
  else begin
    SetPointerAtPosition(High);
    New(NewItem);
    NewItem.Data := param;
    NewItem.Next := nil;
    FCurrently.Next := NewItem;
  end;

  inc(FCount);
end;



procedure TParameterlist.Clear;
var ptemp1,ptemp2: PItem;
begin
  if (FCount > 0) then
  begin
    ptemp1 := FFirst;
    while (ptemp1 <> nil) do
    begin
      ptemp2 := ptemp1.Next;
      Dispose(ptemp1);
      ptemp1 := ptemp2;
    end;
    FCount := 0;
  end;
end;

constructor TParameterlist.Create;
begin
  FFirst := nil;
  FCurrently := nil;

  FCount := 0;
end;

procedure TParameterlist.Delete(index: integer);
begin

end;

destructor TParameterlist.Destroy;
begin

  inherited;
end;

function TParameterlist.GetParameter(index: integer): TParameter;
var i: integer;
begin
  //List index mustn't be higher than the maximum!
  if (index <= High) then
  begin
    //return the first parameter
    if (index = 0) then
      result := FFirst.Data
    else begin
      i := 0;
      //go step by step through the list and fetch the right one!
      FCurrently := FFirst;
      while (i < index) do
      begin
        inc(i);
        FCurrently := FCurrently.Next;

      end;
      result := FCurrently.Data;
    end;
  end
  else result := nil;
end;
    
function TParameterlist.High: integer;
begin
  result := FCount - 1;
end;

procedure TParameterlist.SetParameter(index: integer;
  const Value: TParameter);
begin

end;

procedure TParameterlist.Swap(index1, index2: integer);
begin

end;

procedure TParameterlist.AddAtFirst(Name, DataType: string);
begin

end;

procedure TParameterlist.AddAtPos(Name, DataType: string);
begin

end;

end.
Habe mir dazu eine Testanwendung gebastelt mit 5 Buttons. Wenn ich 6 Items adde, dann alle mit Clear entferne und dann wieder adden will, dann kommt in SetPointerAtPosition an der markierten Stelle eine AV. Wieso?
  Mit Zitat antworten Zitat