Questo sito ha bisogno della pubblicità per poter essere mantenuto. Gentilmente disabilita il blocco della pubblicità nel tuo browser per questo sito web.
Caricamento in corso...Caricamento in corso...
Caricamento in corso...

Coda gestita da lista doppiamente linkata

Creare una coda tramite lista doppiamente linkata.

#include <stdlib.h>
#include <iostream>

using namespace std;

class Coda{
  
  private:
          int valore;//input num
          char a;//input lettera
          Coda *avanti;
          Coda *indietro;
          
  public:
         Coda();//costruttore
         void setNumero(int numero);
         void setParola(char lettera);
         int leggiNumero();
         char leggiParola();
         void pointAtNext(Coda *wheretopoint);
         void pointAtPrevious(Coda *wheretopoint);
         Coda *getNext();
         Coda *getPrevious();          
};

Coda::Coda()
{
            valore=0;
            a='l';
            avanti = NULL;
            indietro  = NULL;
}

void Coda::setNumero(int numero)
{
     valore=numero;     
}

void Coda::setParola(char lettera)
{
     a=lettera;
}

int Coda::leggiNumero()
{
    return valore;
}

char Coda::leggiParola()
{
     return a;
}

void Coda::pointAtNext(Coda *wheretopoint)
{
     avanti=wheretopoint;     
}

void Coda::pointAtPrevious(Coda *wheretopoint)
{
     indietro=wheretopoint;
}

Coda *Coda::getNext()
{
     return avanti;
}

Coda *Coda::getPrevious()
{
     return indietro;
}

int main ()
{


    int numero,scelta;
    char lettera;
    Coda *boxpointer, *start = NULL, *ultimo;
    
    cout<<"CODA CON LISTA DOPPIAMENTE LINKATA - DEBORAH LICCHELLI\n";
    cout<<"-------------------------------------------------------";
    do
    {
         cout<<"\n\n\n";
         cout<<"#1 - inserire elementi nella coda\n";
         cout<<"#2 - visualizzare"<<endl;
         cout<<"#3 - visualizza al contrario"<<endl;
         cout<<"#4 - eliminare primo elemento"<<endl;
         cout<<"#5 - bye bye\n"<<endl;
         cout<<"Scelta---> ";
         cin>>scelta;
         
         switch (scelta)
         {
                case 1:
                     boxpointer = new Coda;
                     cout<<"Inserisci num: ";
                     cin>>numero;
                     boxpointer->setNumero(numero);
                     cout<<"inserisci lettera: ";
                     cin>>lettera;
                     boxpointer->setParola(lettera);
                     
                     if(start==NULL)
                     {
                                    start = boxpointer;
                                    ultimo = boxpointer;
                     }
                      else
                          {
                                    ultimo->pointAtNext(boxpointer);
                                    boxpointer->pointAtPrevious(ultimo);
                                    ultimo = boxpointer;
                          }

                     break;
                     
                case 2:
                     if(start==NULL)
                     {
                                    cout<<"Lista vuota";
                     }
                      else
                          {
                                    boxpointer = start;
                                    cout<<"La lista ha elementi: ";
                                    do
                                    {
                                              cout<<boxpointer->leggiNumero()<<"-";
                                              cout<<boxpointer->leggiParola()<<" ";
                                              boxpointer = boxpointer->getNext();
                                    }while(boxpointer!=NULL);
                          }
                     break;
                     
                case 3:
                     if(ultimo==NULL)
                     {
                                    cout<<"Lista vuota";
                     }
                      else
                          {
                                    boxpointer = ultimo;
                                    cout<<"La lista ha elementi: ";
                                    do
                                    {
                                              cout<<boxpointer->leggiNumero()<<"-";
                                              cout<<boxpointer->leggiParola()<<" ";
                                              boxpointer = boxpointer->getPrevious();
                                    }while(boxpointer!=NULL);
                          }
                     break;
                
                case 4:
                       if(start==NULL)
                       {
                         cout<<"\n[WARNING] La coda e' vuota-> Non si possono cancellare elementi";               
                       }
                        else
                            {
                            cout<<"\n\n[OK!] Il Top della coda "<<start->leggiNumero()<<" e' stato eliminato";
                            cout<<"\n\n[OK!] Il Top della coda "<<start->leggiParola()<<" e' stato eliminato";
                             start = start->getNext();
                        if(start != NULL)
                                    start->pointAtPrevious(NULL);
                             }
                     
                     
                     break;
                     
                case 5:
                     break;
         }
    }while(scelta!=5);
         
         
         
   


cout<<endl;
system("pause");
return(0);
}