简体   繁体   中英

Single linked list - delphi

I am starting to learn programming.

I have to do (HW) program where I can add in example name and surname, add it to single linked list and than display this single linke list.

I tried to do it, program is compiling and it is even working. I can add data and it is displayed. But I propably dont understan something and make mistake. I am adding first person, it is displayed, than I am adding second person, it is also displayed but the first person data is override by the second person I added. So I have two the same records. Listbox is only showing data from the single linked list so I suppose there is no problem. Each pointer should point different data so why each record is the same as the last one I added?

Here is my code:

    type
  wskaznik = ^Lista;

  Lista = record
    lp : string;
    dane : string;
    wsk : wskaznik;
  end;

var
  Form1 : TForm1;
  First, current : wskaznik;
  tekst : string;
  liczba : string;
  i, k : integer;


implementation

{$R *.lfm}

{ TForm1 }

procedure AddToList(dane : string; lp : string; var current : wskaznik);
var
  prev, Next : wskaznik;
begin
  if current <> nil then
  begin
    prev := current;
    Next := current^.wsk;
  end
  else
  begin
    prev := nil;
    Next := nil;
  end;

  new(current);
  current^.dane := dane;
  current^.lp := lp;
  current^.wsk := Next;
  if prev <> nil then
    prev^.wsk := current;
end;


procedure GetAddr(dane : string; var First, current : wskaznik);
var
  Next : wskaznik;
begin
  if First <> nil then
  begin
    Next := First;
    repeat
      if Next^.wsk <> nil then
        Next := Next^.wsk
    until (Next^.wsk = nil) or (Next^.dane = dane);
    current := Next;
  end;
end;


procedure GetNum(n : integer; var First, current : wskaznik);
var
  Next : wskaznik;
begin
  if First <> nil then
    if n = 1 then
      current := First
    else
    if (n = 2) and (First^.wsk = nil) then
      n := 0
    else
    begin
      Next := First;
      i := 1;
      repeat
        Inc(i);
        if Next^.wsk <> nil then
          Next := Next^.wsk
      until (i = n) or (Next^.wsk = nil);
      if (Next^.wsk = nil) and (i < n) then
        n := 0
      else
        current := Next;
    end;
end;



procedure List;
var
  l : integer;
begin
  form1.listbox1.Clear;
  form1.listbox2.Clear;
  for l := 1 to i do
  begin
    Getnum(l, First, current);
    if l > 1 then
      form1.listbox1.items.add(current^.dane);
    form1.listbox2.items.add(current^.lp);
  end;
end;


procedure findLess(dane : string; lp : string; var First, current : wskaznik);
var
  tmp, Next : wskaznik;
begin
  if First <> nil then
  begin
    Next := First;
    repeat
      if (Next^.wsk <> nil) then
      begin
        tmp := Next;
        Next := Next^.wsk;
      end;
    until (Next^.wsk = nil) or (Next^.dane > dane);
    if Next^.dane > dane then
      current := tmp
    else
      current := Next;
    if Next^.lp > lp then
      current := tmp
    else
      current := Next;
  end;
end;

procedure TForm1.Button1Click(Sender : TObject);
begin
  Inc(i);
  findLess(edit1.Text, edit2.Text, First, current);
  addtolist(edit1.Text, edit2.Text, current);
  label3.Caption := 'Elementów: ' + IntToStr(i - 1);
  //edit1.SetFocus;
  list;
end;






end. 

You never assign anything to First (assuming First is to be the beginning of the list). The first time you call AddToList you should assign Current to First

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM