{Satu Elorannan versio tehtävään 11.6:  Päivitetään linkkejä! }

program h116(input, output);

type os = ^tietue;

     tietue = record
                  tieto: integer;
                  linkki: os;
              end;

procedure liita(var x,y: os);

var a: os;
begin
   if x = nil then
   begin
      x := y;
      y := nil;
   end
   else
   begin
      a := x;
      while a^.linkki <> nil do a := a^.linkki;
      a^.linkki := y;
      y := nil;
   end;
end;

function suurin(x: os): os;

var a,b: os;
begin
   a:= x; b:=x;
   while a <> nil do
   begin
      if a^.tieto > b^.tieto then b := a;
      a := a^.linkki;
   end;
   suurin := b;
end;

procedure poista(var x: os; y: os);

var a: os;
begin
   if x = y then x := x^.linkki
   else
   begin
     a := x;   
     while a^.linkki <> y do a := a^.linkki;
     a^.linkki := y^.linkki;
     y^.linkki := nil;
   end;
end;

procedure jarjesta(var x: os);

var uusi,iso: os;
begin
   uusi := nil;
   while x <> nil do
   begin
      iso := suurin(x);
      poista(x,iso);
      iso^.linkki := uusi;
      uusi := iso;
   end;
   x := uusi;
end;

begin
end.

