Lista bidirezionale

Puntatore di testa, puntatore di coda e tre nodi con puntatori nelle due direzioni

image

Dichiarazioni

Type
   tInfo   = "qualsiasi"; {le informazioni nel nodo}
   pNodoDP = ^NodoDP;
   NodoDP  = Record
      Info: tInfo;
      Succ,                {puntatori nelle due direzioni}
      Prec: pNodoDP;
   End;
   ListaDP = Record
      Testa,
      Coda: pNodoDP;
   End;

Operazioni

Aggiungere un nodo in Testa

Procedure AggInTesta(var L: ListaDP; X: tInfo);
Var
   P: pNodoDP;
Begin
   New(P);
   P^.Info:=X;
   P^.Prec:=Nil;
   P^.Succ:=L.Testa;
   If(L.Testa <> Nil) Then
      L.Testa^.Prec:=P
   Else
      L.Coda:=P;
   L.Testa:=P;
End;

Aggiungere un nodo in Coda

Procedure AggInCoda(Var L: ListaDP; X: tInfo);
Var
   P: pNodoDP;
Begin
   New(P);
   P^.Info:=X;
   P^.Succ:=Nil;
   P^.Prec:=L.Coda;
   If(L.Coda <> Nil) Then
      L.Coda^.Succ:=P
   Else
      L.Testa:=P;
   L.Coda:=P;
End;

Aggiungere un nodo in Ordine

Procedure AggInOrdine(Var L: ListaDP; X: tInfo);
Var
   P, Q: pNodoDP;
Begin
   New(P);
   P^.Info:=X;
   If(L.Testa = Nil) Or (X <= L.Testa^.Info) Then
      Begin
         P^.Prec:=Nil;               {...aggiungi primo nodo...}
         P^.Succ:=L.Testa;
         If(L.Testa = Nil) Then      {...aggiungi in testa...}
            L.Coda:=P;
         Else
            L.Testa^.Prec:=P;
         L.Testa:=P;
      End
   Else if(L.Coda^.Info <= X) Then
      Begin
         P^.Prec:=L.Coda;            {...aggiungi in coda...}
         P^.Succ:=Nil;
         L.Coda^.Succ:=P;
         L.Coda:=P;
      End
   Else
      Begin
         Q:=L.Testa^.Succ;           {...aggiungi prima di Q...}
         While(Q^.Info < X) Do
            Q:=Q^.Succ;
         P^.Prec:=Q^.Prec;
         P^.Succ:=Q;
         Q^.Prec^.Succ:=P;
         Q^.Prec:=P;
      End;
End;