1

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. 
5
  • This program has to allow me to add two words (in example name and surname) to single linked list and display that list. But when i try to fill the list each time I am adding "name" list is filled with the same names. Commented Jan 22, 2015 at 18:32
  • 4
    This is time to learn how to debug Commented Jan 22, 2015 at 20:11
  • I suspect in edit1 you enter first name, and in edit2 the surname? It would help if you explained what is to appear in the two list boxes. Commented Jan 22, 2015 at 20:14
  • Edit1 one is filled with Name and than it appears in List1; Edit2 is filled with surname and than it appears in list2. Ok but how I should use the debugger? Application is compiling without error? So I supposed I made some error which is assigning data incorrectly, but it does not crash application. Commented Jan 22, 2015 at 20:24
  • 2
    My program compiles, but it doesn't do what you want your program to do. Compilation means little. Runtime behaviour is what matters. Learning to debug is critical. Inspect your program at runtime. Any one of us can tell you what is wrong with your program. What will be useful to you is the ability for you to debug. Commented Jan 22, 2015 at 20:33

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.