We help IT Professionals succeed at work.

I am trying to sort a linked list, but my program doesn't print anything. Help please!

Lucas Hornung
Lucas Hornung used Ask the Experts™
on
So I am trying to create a program that takes a list of integers typed in by the user, puts them into order and prints them to the screen. However, my compiler just shows nothing after I type in the list. No output whatsoever.
I get the following hint: Variable "RefListe" does not seem to be initialized. But I cannot make sense of that right now.

Can anyone tell me why this doesn't work?

program TesteSortiereListe(input, output);

  type
  tNatZahl = 0..maxint;
  tRefListe = ^tListe;
  tListe = record
             info : tNatZahl;
             next : tRefListe;
           end;

  var
  RefListe : tRefListe;


procedure SortiereListe (var ioRefListe:tRefListe);
{ Sorts a linked list in ascending order }

  var
  RefAnfang,
  Vorgaenger,
  aktuell:tRefListe;

begin
  RefAnfang := ioRefListe;
  while ioRefListe <> nil do
  {Go through the entire list}
  begin
  vorgaenger := ioRefListe;
  ioRefListe := ioRefListe^.next;
  aktuell := ioRefListe;
    if aktuell^.info < RefAnfang^.info then
    {Element is the smallest element found so far}
      begin
      Vorgaenger^.next := aktuell^.next;
      aktuell^.next := RefAnfang;
      RefAnfang := aktuell;
      end
    else if ioRefListe^.info > aktuell^.info then
    { Element is the biggest element in the list so far, we don't have to change its position}
      begin
      aktuell:=ioRefListe;
      end
    else
        if aktuell^.info < vorgaenger^.info then
        {The number is smaller than the previous one, they have to be exchanged}
          begin
          vorgaenger^.next := aktuell^.next;
          aktuell^.next := vorgaenger;
          vorgaenger := aktuell;
          end {if}
    end

end; {SortiereListe}
 procedure Anhaengen(var ioListe : tRefListe;
                        inZahl : tNatZahl);
{ Inserts a new integer into the list }
  var Zeiger : tRefListe;
begin
  Zeiger := ioListe;
  if Zeiger = nil then
  begin
    new(ioListe);
    ioListe^.info := inZahl;
    ioListe^.next := nil;
  end
  else
  begin
    while Zeiger^.next <> nil do
      Zeiger := Zeiger^.next;
    { Jetzt zeigt Zeiger auf das letzte Element }
    new(Zeiger^.next);
    Zeiger := Zeiger^.next;
    Zeiger^.info := inZahl;
    Zeiger^.next := nil;
  end;
end;

procedure ListeEinlesen(var outListe:tRefListe);
{ reads the list typed in in Anhaengen and saves it inside RefListe }
  var
  Liste : tRefListe;
  Zeile : string;
  Zahl, Code : integer;
begin
  writeln('Bitte geben Sie die zu sortierenden Zahlen ein.');
  writeln('Beenden Sie Ihre Eingabe mit einer Leerzeile.');
  Liste := nil;
  readln(Zeile);
  val(Zeile, Zahl, Code); { val converts String into Integer }
  while Code=0 do
  begin
    Anhaengen(Liste, Zahl);
    readln(Zeile);
    val(Zeile, Zahl, Code);
  end; { while }
  outListe := Liste;
end; { ListeEinlesen }

procedure GibListeAus(inListe : tRefListe);
{ Prints the Elements inside inList }
  var Zeiger : tRefListe;
begin
  Zeiger := inListe;
  while Zeiger <> nil do
  begin
    writeln(Zeiger^.info);
    Zeiger := Zeiger^.next;
  end; { while }
end; { GibListeAus }

begin
  ListeEinlesen(RefListe);
  SortiereListe(RefListe);
  GibListeAus(RefListe);
  readln;
end. 

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Oracle dba
Top Expert 2009
Commented:
you are passing the list as a var parameter.
you should pass the list as a normal parameter (without the var)

the variable RefListe points to the first element in the list ... you don't really want it point anywhere else
with a var parameter you could be returned a different list, without the var, only the content changes

this is old school stuff
you really want to keep working with pointers ?
nowadays, you'd use a TList object and add items
and in the end sort it, or use a TSortedList
https://spring4d.4delphi.com/docs/develop/Html/index.htm?Spring.Collections.Lists.TSortedList(T).htm
Geert GOracle dba
Top Expert 2009

Commented:
if you want to know everything about this, read chapter 3 of Julian's book: Tomes of Delphi
https://www.e-reading.club/bookreader.php/142043/Bucknall_-_The_Tomes_Of_Delphi_Algorithms_And_Data_Structures.pdf

That explains in depth how and why of Linked Lists