SHARE
TWEET

ASD Pb 18 liste stergere pare

madalinaradu May 26th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** p18 lista stergere pare*/
  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 *p=NULL){
  13.         this->info=info;
  14.         this->next=next;
  15.     }
  16.     int getInfo(){
  17.         return info;
  18.     }
  19.     friend class Lista;
  20. };
  21.  
  22. class Lista{
  23. private:
  24.     Nod *prim;
  25.     Nod * ultim;
  26. public:
  27.     Lista(){
  28.         prim=ultim=NULL;
  29.     }
  30.     void adaugaLaSfarsit(int info);
  31.     void afisare();
  32.     void stergerePare();
  33. };
  34.  
  35. void Lista:: adaugaLaSfarsit(int info){
  36.     Nod *p=new Nod(info);
  37.     if(prim==NULL){
  38.         prim=ultim=p;
  39.     }else{
  40.         p->next=NULL;
  41.         ultim->next=p;
  42.         ultim=p;
  43.     }
  44. }
  45. void Lista:: afisare(){
  46.     Nod *p=prim;
  47.     cout<<"[";
  48.     while(p!=NULL){
  49.         cout<<p->info<<"   ";
  50.         p=p->next;
  51.     }
  52.     cout<<"]";
  53. }
  54.  
  55. void Lista:: stergerePare(){
  56.     while(prim!=NULL && prim->info%2==0){
  57.         Nod *temp=prim;;
  58.         prim=prim->next;
  59.         delete temp;
  60.     }
  61.     if(prim!=NULL){
  62.         Nod *p=prim;
  63.         Nod *q=p->next;///ordinea este p,q
  64.         while(q!=NULL){
  65.             if(q->info%2==0){
  66.                 p->next=q->next;
  67.                 delete q;
  68.                 q=p->next;
  69.             }else{
  70.                 p=q;
  71.                 q=q->next;
  72.             }
  73.         }
  74.     }
  75. }
  76.  
  77. int main(){
  78.     Lista lista;
  79.     lista.adaugaLaSfarsit(8);
  80.     lista.adaugaLaSfarsit(2);
  81.     lista.adaugaLaSfarsit(10);
  82.     lista.adaugaLaSfarsit(4);
  83.     lista.adaugaLaSfarsit(11);
  84.     lista.adaugaLaSfarsit(6);
  85.     lista.afisare();
  86.     lista.stergerePare();
  87.     lista.afisare();
  88.     return 0;
  89. }
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