Lista con testa e coda

Puntatore di testa, puntatore di coda e tre nodi con informazioni A-B-C
image

Dichiarazioni

Type
   tInfo   = "qualsiasi"; {le informazioni nel nodo}
   pNodoTC = ^NodoTC;
   NodoTC  = Record
      Info: tInfo;
      Succ: pNodoTC;
   End;
   ListaTC = Record       {puntatori di testa e di coda}
      Testa,
      Coda: pNodoTC;
   End;

Operazioni

Aggiungere un nodo in Testa

procedure AggInTesta(var L: ListaTC; X: tInfo);
Var
   P: pNodoTC;
Begin
   New(P);
   P^.Info:=X;
   P^.Succ:=L.Testa;
   If(L.Testa = Nil) Then
      L.Coda:=P;
   L.Testa:=P;
End;

Aggiungere un nodo in Coda

procedure AggInCoda(Var L: ListaTC; X: tInfo);
Var
   P: pNodoTC;
Begin
   New(P);
   P^.Info:=X;
   P^.Succ:=Nil;
   If(L.Testa = Nil) Then
      L.Testa:=P
   Else
      L.Coda^.Succ:=P;
   L.Coda:=P;
End;

Aggiungere un nodo in Ordine

procedure AggInOrdine(Var L: TListaTC; X: tInfo);
Var
   P, Q, R: pNodoTC;
Begin
   New(P);
   P^.Info:=X;
   If(L.Testa = Nil) Or (X <= L.Testa^.Info) Then
      Begin
         P^.Succ:=L.Testa;           {...aggiungi primo nodo...}
         L.Testa:=P;
         If(L.Coda = Nil) Then       {...aggiungi in testa...}
            L.Coda:=P;
      End
   Else If(L.Coda^.Info <= X) Then
      Begin
         P^.Succ:=Nil;               {...aggiungi in coda...}
         L.Coda^.Succ:=P;
         L.Coda:=P;
      End
   Else
      Begin
         Q:=L.Testa;                 {...aggiungi Prima di R...}
         R:=L.Testa^.Succ;
         While(R^.Chiave < N.Chiave) Do
            Begin
               Q:=R;
               R:=R^.Succ;
            End;
         Q^.Succ:=P;
         P^.Succ:=R;
      End;
End;

Eliminare un nodo in Testa

procedure EliInTesta(Var L: ListaTC);
Var
   P: pNodoTC;
Begin
   If(L.Testa <> Nil) Then
      Begin
         P:=L.Testa;
         L.Testa:=L.Testa^.Succ;
         If(L.Testa = Nil) Then
            L.Coda:=Nil;
         Dispose(P);
      End;
End;