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...

Definire la classe lista linkata e la classe lista

Definire la classe lista linkata e la classe lista doppiamente linkata.
Memorizzare i dati (non è detto numeri naturali o caratteri, possibili coppie di tipi differenti)    in modo ordinato  usando le due liste. 
Stampare  le liste.
Verificare se un elemento è stato memorizzato.

 

#include<iostream>
#include<stdlib.h>
using namespace std;
/*
Definire la classe lista linkata e la classe lista doppiamente linkata.
Memorizzare i dati (non è detto numeri naturali o caratteri, possibili coppie di tipi differenti)    in modo ordinato  usando le due liste. 
Stampare  le liste.
Verificare se un elemento è stato memorizzato.
Eliminare un elemento.*/


class ListaLink{
      
              int u;
              ListaLink *next;
              
      public:
             ListaLink(void);
             ListaLink *getNext(void);
             int getU(void);
             void setNext(ListaLink *p);
             void setU(int v);   
             void stampa();
             int canclista();
};

ListaLink::ListaLink(void)
{
    next = NULL;
    u = 0;                                               
}

ListaLink *ListaLink::getNext(void)
{
   return next;
}

int ListaLink::getU(void)
{
    return u;
}

void ListaLink::setNext(ListaLink *p)
{
     next = p;     
}

void ListaLink::setU (int v)
{
     u = v;     
}

void ListaLink::stampa()
{
     cout<<u<<" ";     
}
      //classe lista doppiamente linkata
class ListaDoppia{
         int u;
              ListaDoppia *next, *prev;
              
      public:
             ListaDoppia(void);
             ListaDoppia *getNext(void);
             ListaDoppia *getPrev(void);
             int getU(void);
             void setNext(ListaDoppia *p);
             void setPrev(ListaDoppia *p);
             void setU(int v);   
             void stampa();
};    

ListaDoppia::ListaDoppia()
{
         u = 0;
         next = NULL;
         prev = NULL;                    
}

ListaDoppia *ListaDoppia::getNext()
{
           return next;            
}

ListaDoppia *ListaDoppia::getPrev()
{
           return prev;            
}

int ListaDoppia::getU(void)
{
  return u;    
}

void ListaDoppia::setNext(ListaDoppia *p)
{
     next = p;     
}

void ListaDoppia::setPrev(ListaDoppia *p)
{
     prev = p;     
}

void ListaDoppia::setU(int v)
{
     u = v;
}

int main()
{
    int valore, i, el, n, scelta;
    ListaLink *Pointer;//puntatore usato per la creazione delle scatole o per la stampa
    ListaLink *start = NULL;//puntatore di inizio lista
    ListaLink *temp, *temp2;//puntatori temporanei
    ListaDoppia *Ppointer;//puntatore usato per la creazione delle scatole o per la stampa
    ListaDoppia *sstart = NULL;//puntatore di inizio lista
    ListaDoppia *ttemp, *ttemp2;//puntatori temporanei
    bool flag;
    
    
          cout<<"Licchelli Deborah - Tipo di Traccia 1\n";
    
    do
      {
          cout<<"\n\n- Per inserire un elemento nelle liste e ordinarle digita 1.";
          cout<<"\n- Per visualizzare la lista digita 2.";
          cout<<"\n- Per ricercare un numero digita 3.";
          cout<<"\n- Per uscire dal programma digita 5.";
          cout<<"\nDigita ora la tua scelta:";
          cin>>scelta;
          
          switch(scelta)
          {
                        case 1:
                               Pointer = new ListaLink; // alloca nuova scatola
                               cout<<"\nInserire il valore della scatola:";
                               cin>>valore;//lettura del valore da attribuire all'attribbuto
                               Pointer->setU(valore);//settaggio valore
                               if(start == NULL)                
                               {
                                        start = Pointer; //se start=NULL, la scatola creata è la prima
                                        }//primo if
                                else
                                {
                                    temp=start;//
                                    temp2=temp;
                                    while((temp->getNext()!=NULL) && (temp->getU() < valore))//fino a quando temp non punta null e il suo valore è < di val
                                    {
                                                                  temp2=temp; 
                                                                  temp=temp->getNext();
                                                                  }//fine while
                                      if(valore<start->getU()) //se valore inserito < valore contenuto in start
                                       {
                                                            Pointer->setNext(start); //la nuova scatola punta start
                                                            start=Pointer;
                                                            }//fine if 2
                                        else if(start->getNext()!=NULL) //se start non punta null
                                             {
                                                               if(temp->getU()>= valore)//se il valore contenuto in temp è >= a quello inserito
                                                               {
                                                                                 Pointer->setNext(temp);//la nuova scatola punta a temp
                                                                                 temp2->setNext(Pointer);//temp2 punta alla nuova scatola
                                                                                 }//fine if4
                                                                else
                                                                {
                                                                    temp->setNext(Pointer);//temp punta alla nuova scatola
                                                                    }//else4
                                                                }//fine if 3
                                       else
                                         {
                                              start->setNext(Pointer);//start punta alla nuova scatola
                                                 }//else2
                                    }//fine else 3
                                  
                                   
                                   //_______________________________//
                                   Ppointer = new ListaDoppia; // alloca nuova scatola
                               cout<<"\nInserire il valore della scatola:";
                               cin>>valore;//lettura del valore da attribuire all'attribbuto
                               Ppointer->setU(valore);//settaggio valore
                               if(sstart == NULL)                
                               {
                                        sstart = Ppointer; //se start=NULL, la scatola creata è la prima
                                        }//primo if
                                else
                                {
                                    ttemp=sstart;//
                                    ttemp2=ttemp;
                                    while((ttemp->getNext()!=NULL) && (ttemp->getU() < valore))//fino a quando temp non punta null e il suo valore è < di val
                                    {
                                                                  ttemp2=ttemp; 
                                                                  ttemp=ttemp->getNext();
                                                                  }//fine while
                                      if(valore<sstart->getU()) //se valore inserito < valore contenuto in start
                                       {
                                                            sstart->setPrev(Ppointer);
                                                            Ppointer->setNext(sstart); //la nuova scatola punta start
                                                            sstart=Ppointer;
                                                            }//fine if 2
                                        else if(sstart->getNext()!=NULL) //se start non punta null
                                             {
                                                               if(ttemp->getU()>= valore)//se il valore contenuto in temp è >= a quello inserito
                                                               {
                                                                                 ttemp->setPrev(Ppointer);
                                                                                 Ppointer->setNext(ttemp);//la nuova scatola punta a temp
                                                                                 Ppointer->setPrev(ttemp2);
                                                                                 ttemp2->setNext(Ppointer);//temp2 punta alla nuova scatola
                                                                                 }//fine if4
                                                                else
                                                                {
                                                                    Ppointer->setPrev(ttemp);
                                                                    ttemp->setNext(Ppointer);//temp punta alla nuova scatola
                                                                    }//else4
                                                                }//fine if 3
                                       else
                                         {
                                              Ppointer->setPrev(sstart);
                                              sstart->setNext(Ppointer);//start punta alla nuova scatola
                                                 }//else2
                                    }//fine else 3
                                   break;
                                   
                                   
                                   
                                   
                         
                       case 2:    
                               if(start==NULL)//se start==null non ci sono scatole
                                 {
                                               cout<<"\n\nLa lista e' vuota.\n";
                                               }
                                 else
                                 {
                                     Pointer=start;//la stampa parte da start
                                      cout<<"\n\n La lista contiene i seguenti valori:";
                                      do
                                      {
                                            Pointer->stampa();//stampa boxPointer
                                               Pointer=Pointer->getNext();//boxPointer scala nella lista
                                               }
                                               while(Pointer!=NULL);//il ciclo si ripete fino a quando boxPointer punta a null
                                               }
                                     
                       //____________________stampa delle seconda lista doppia___________//
                       if(sstart==NULL)//se start==null non ci sono scatole
                                 {
                                               cout<<"\n\nLa lista e' vuota.\n";
                                               }
                                 else
                                 {
                                     Ppointer=sstart;//la stampa parte da start
                                      cout<<"\n\n La lista contiene i seguenti valori:";
                                      do
                                      {
                                                  cout<<Ppointer->getU()<<" ";
                                               Ppointer=Ppointer->getNext();//boxPointer scala nella lista
                                               }
                                               while(Ppointer!=NULL);//il ciclo si ripete fino a quando boxPointer punta a null
                                               }
                                      break;
                       
                       
                             
                              case 3:
                             
                             cout<<"Inserisci l'elemento da ricercare: ";
                             cin>>n;
                             flag = false;
                             if(start==NULL)//se start==null non ci sono scatole
                                 {
                                               cout<<"\n\nLa lista e' vuota.\n";
                                               }
                                 else
                                 {
                                       Pointer=start;//la stampa parte da start
                                     
                                      
                                      do
                                      {
                                               if(Pointer->getU() == n)
                                                  flag = true;
                                             else
                                             flag = false;
                                               Pointer=Pointer->getNext();//boxPointer scala nella lista
                                               }
                                               while(Pointer!=NULL && !flag);//il ciclo si ripete fino a quando boxPointer punta a null
                                               }
                                      if(flag)
                                          cout<<"Valore trovato\n";
                                      else
                                      cout<<"Valore non trovato\n";
                             //_______________________//Elemento da cercare nella listadoppialinkata
                                      
                                 cout<<"Inserisci l'elemento da ricercare: ";
                             cin>>n;
                             flag = false;
                             if(start==NULL)//se start==null non ci sono scatole
                                 {
                                               cout<<"\n\nLa lista e' vuota.\n";
                                               }
                                 else
                                 {
                                       Ppointer=sstart;//la stampa parte da start
                                     
                                      
                                      do
                                      {
                                               if(Ppointer->getU() == n)
                                                  flag = true;
                                             else
                                             flag = false;
                                               Ppointer=Ppointer->getNext();//boxPointer scala nella lista
                                               }
                                               while(Ppointer!=NULL && !flag);//il ciclo si ripete fino a quando boxPointer punta a null
                                               }
                                      if(flag)
                                          cout<<"Valore trovato\n";
                                      else
                                      cout<<"Valore non trovato\n";
                                      break;
                                               
                                      
                     
                      
                      }   while(scelta!=5);
    
    system("PAUSE");
}