SHARE
TWEET

ASD Pb 17 liste leg inversate

madalinaradu May 26th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** p17 legaturi lista inversate*/
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. class Lista;
  6.  
  7. class Nod{
  8. private:
  9.     int info;
  10.     Nod *next;
  11. public:
  12.     Nod(int info = 0, Nod *next=NULL){
  13.         this->info=info;
  14.         this->next-next;
  15.     }
  16.     int getInfo(){
  17.         return info;
  18.     }
  19.     friend class Lista;
  20. };
  21.  
  22.  
  23. class Lista{
  24. private:
  25.     Nod *prim;
  26.     Nod *ultim;
  27. public:
  28.     Lista(){
  29.         prim=ultim=NULL;
  30.     }
  31.     void adaugaLaSfarsit(int info);
  32.     void afisare();
  33.     void inversareLista();
  34. };
  35.  
  36. void Lista::adaugaLaSfarsit(int info){
  37.     Nod *p=new Nod(info);
  38.     if(prim==NULL){
  39.         prim=ultim=p;
  40.     }else{
  41.         p->next=NULL;
  42.         ultim->next=p;
  43.         ultim=p;
  44.     }
  45. }
  46.  
  47. void Lista:: afisare(){
  48.     Nod *p=prim;
  49.     cout<<"[";
  50.     while(p!=NULL){
  51.         cout<<p->info<<"  ";
  52.         p=p->next;
  53.     }
  54.     cout<<"]";
  55. }
  56.  
  57. void Lista::inversareLista(){
  58.     Nod *r, *p, *q;
  59.     r=NULL;
  60.     p=prim;
  61.     q=NULL;
  62.     ultim=p;
  63.     while(p!=NULL){
  64.         q=p->next;
  65.         p->next=r;
  66.         r=p;
  67.         p=q;
  68.     }
  69.     prim=r;
  70. }
  71.  
  72. int main(){
  73.     Lista lista;
  74.     lista.adaugaLaSfarsit(1);
  75.     lista.adaugaLaSfarsit(2);
  76.     lista.adaugaLaSfarsit(3);
  77.     lista.adaugaLaSfarsit(4);
  78.     lista.adaugaLaSfarsit(5);
  79.     lista.afisare();
  80.     lista.inversareLista();
  81.     lista.afisare();
  82.     lista.adaugaLaSfarsit(6);
  83.     lista.afisare();
  84.     return 0;
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top