简体   繁体   English

单链表-Delphi

[英]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. 我必须执行(HW)程序,在其中可以添加示例名称和姓氏,将其添加到单个链接列表中,然后显示此单个链接列表。

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). 您永远不会为“ First分配任何内容(假设“ First将成为列表的开头)。 The first time you call AddToList you should assign Current to First 第一次调用AddToList时,应将Current分配给First

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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