unit Un_list1;

interface
 uses Un_company;
 type TList=^TNodeL;
      TNodeL=record
              Company:TCompany;
              next:TList
             end;
                procedure InitLL (var L:TList);
                function EmptyLL (L:TList):boolean;
                procedure PrintLL(L:TList);
                procedure ClearLL (var L:TList);
                function Found_beforeLL (var L:TList;namecompany:string):TList;
                procedure Delete_afterLL  (var H:TList);
                procedure Add_afterLL (p:TList;namecompany:string);
implementation
  procedure InitLL (var L:TList);
  begin
    new(L);
    L^.next:=nil
  end;

  function EmptyLL (L:TList):boolean;
  begin
    result:=L^.next=nil
  end;

  function Found_beforeLL (var L:TList;namecompany:string):TList; //поиск компании по наименованию (адрес предыдущего элемента)
  var p:TList;
  begin
    p:=L;
    while (p^.next <> nil) and  (P^.next^.company.comparecompany(namecompany)<0) do
    p := p^.next;
    result:=p
  end;

  procedure Delete_afterLL (var H:TList);
  var P:TList;
  begin
    p:=H^.next;
    H^.next:=p^.next;
    p^.Company.Destroy;
    Dispose(p)
  end;

   procedure Add_afterLL (p:TList;namecompany:string);
   var q:TList;
   begin
     New(q);
     q^.Company:=TCompany.Create(namecompany);
     q^.next:=p^.next;
     p^.next:=q
   end;

  procedure PrintLL (L:TList);
  var p:TList;
  begin
    p:=L^.next;
    while p<>nil do
     begin
       p^.company.Print;
       p:=p^.next
     end
  end;

  procedure ClearLL (var L:TList);
  begin
    // удаление элементов списка
    while not EmptyLL(L) do
     Delete_afterLL(L);
    // удаление заглавного звена
    Dispose(L)
  end;

end.
