unit Un_list;

interface
  uses
  Un_Abonent;
  type TList=^TNode;
       TNode=record
              abonent:TAbonent;
              next:TList
             end;
                procedure Init (var L:TList);
                function EmptyL (L:TList):boolean;
                procedure Add_afterL (p:TList;abonent:TAbonent);
                procedure Delete_afterL(var H:TList); // удаление элемента из головы списка
                procedure Add_OrderL(var L:TList; new_elem:TAbonent);
                function Del_abonentL(var L:TList;numberphone:TAbonent):boolean;
                procedure PrintL (L:TList);
                procedure ClearL (var L:TList);
                function add_abonentL(l:Tlist; abonent:TAbonent):Boolean;
                function Search(var L:TList;abonent:Tabonent):integer;
implementation


  procedure Init (var L:TList);
  begin
    new(L);
    L^.next:=nil
  end;

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

  procedure Add_afterL(p:TList; abonent:TAbonent);
  var q:TList;
  begin
    new(q);
    q^.abonent:=abonent;
    q^.next:=p^.next;
    p^.next:=q
  end;

  procedure Add_OrderL(var L:TList; new_elem:TAbonent);
  var p:TList;
  begin
   // поиск места для вставки
    p:=L;
    while (p^.next<>nil) and (p^.next^.abonent.compare(new_elem)<0) do  //(p^.next^.info.inf1<=new_elem.inf1)
    p:=p^.next;
   // вставка нового элемента
    Add_afterL(p,new_elem);
  end;

  function Add_abonentL;
  var p:TList;
  begin
    p:=L;
    while (p^.next<>nil)and (p^.next^.abonent.Compare(abonent)<0) do
    p:=p^.next;
    if (p^.next<>nil)and (p^.next^.abonent.Compare(abonent)=0) then
                                                       begin
                                                         result:=True;
                                                         abonent.Destroy
                                                        end
                                                     else  result:=False
  end;

  function Del_abonentL(var L:TList;numberphone:TAbonent):boolean;
  var p, q : TList;
  begin
     P := L;
     while (p^.next <> nil) and  (P^.next^.abonent.compare(numberphone)<0) do
     p := p^.next;
     if p^.next <> nil then
     begin
       result:=true;
       q := p^.next;
       p^.next := q^.next;
       q^.abonent.Destroy;
       Dispose(q)
     end
     else
     result:=false
  end;

  procedure Delete_afterL (var H:TList);
  var p:TList;
  begin
    p:=H^.next;
    H^.next:=p^.next;
    p^.abonent.Destroy;
    dispose(p)
  end;

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

  function Search;
  var k:integer;
      p:TList;
      h:boolean;
 begin
    k:=0;
    p:=L^.next;
    h:=p^.abonent.stop(abonent); // p^.info>s
    while (p<>nil) and not h do
     begin
       if p^.abonent.Comparenumber(Abonent) then
                                                 k:=k+1;
       if p^.abonent.stop(Abonent) then h:=true
                          else p:=p^.next
     end;
 end;

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

end.
