Advertisement
madalinaradu

Lista bubble

May 9th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. #include<math.h>
  5. #include<iostream>
  6. using namespace std;
  7. class Lista {
  8. private:
  9.     struct Nod {
  10.         int info;
  11.         struct Nod *next;
  12.     };
  13.     Nod* prim;
  14.  
  15. public:
  16.     Lista();
  17.     void adaugare(int x);
  18.     void afisare();
  19.     void suma();
  20.     void stergerePare();
  21.     void inversareLista();
  22.     void bubble();
  23.  
  24.     void swap(Nod *a, Nod *b){
  25.         int temp=a->info;
  26.         a->info=b->info;
  27.         b->info=temp;
  28.     }
  29.  
  30. };
  31.  
  32. Lista::Lista() {
  33.     prim=NULL;
  34.  
  35. }
  36.  
  37. void Lista:: adaugare(int x) {//la coada
  38.     Nod * v;
  39.     v=new Nod;
  40.     v->info=x;
  41.     v->next=NULL;
  42.     if(prim==NULL) {
  43.         prim=v;
  44.     } else {
  45.         Nod* temp=prim;
  46.         while(temp->next!=NULL) {
  47.             temp=temp->next;
  48.         }
  49.         temp->next=v;
  50.     }
  51. }
  52. void Lista::afisare() {
  53.     Nod *temp=prim;
  54.     while(temp!=NULL) {
  55.         cout<<temp->info<<"   ";
  56.         temp=temp->next;
  57.     }
  58.     cout<<endl;
  59. }
  60.  
  61. void Lista::suma() {
  62.     if(prim!=NULL && prim->next!=NULL) {
  63.         Nod * q,*p;//parcurge lista
  64.         q=prim;
  65.         p=q->next;
  66.         while(p!=NULL) {
  67.             Nod *temp;// in el se calculeaza suma a 2 elem consecutive;
  68.             temp=new Nod;
  69.             temp->info=p->info + q->info;
  70.             temp->next=p;
  71.             q->next=temp;
  72.             q=p;
  73.             p=p->next;
  74.         }
  75.  
  76.     }
  77.  
  78. }
  79.  
  80. void Lista::stergerePare() {
  81.     while(prim!=NULL && prim->info%2==0) {
  82.         Nod *temp=prim;
  83.         prim=prim->next;
  84.         delete temp;
  85.  
  86.     }
  87.     if(prim!=NULL) {
  88.         Nod*p=prim;
  89.         Nod*q=prim->next;
  90.         while(q!=NULL ) {
  91.             if(q->info%2==0) {
  92.                 p->next=q->next;
  93.                 delete q;
  94.                 q=p->next;
  95.             } else {
  96.                 p=q;
  97.                 q=q->next;
  98.             }
  99.  
  100.         }
  101.  
  102.     }
  103. }
  104. void Lista::inversareLista() {
  105.     Nod *r, *p, *q;
  106.     r=NULL;
  107.     p=prim;
  108.     q=NULL;
  109.  
  110.     while(p!=NULL) {
  111.         q=p->next;
  112.         p->next=r;
  113.         r=p;
  114.         p=q;
  115.     }
  116.     prim=r;
  117. }
  118.  
  119. void Lista::bubble(){
  120.     int ok=0;
  121.     Nod *p, *q;
  122.  
  123.     if(prim==NULL){
  124.         return;
  125.     }else{
  126.         do{
  127.             ok=0;
  128.              p=prim;
  129.              while(p->next!=NULL){
  130.                 q=p->next;
  131.                 if(p->info > q->info){
  132.                    swap(p,q);
  133.                    ok=1;
  134.                 }
  135.                 p=p->next;
  136.              }
  137.  
  138.         }while(ok);
  139.     }
  140. }
  141.  
  142.  
  143. int main() {
  144.     int n=0;
  145.     Lista pare, impare;
  146.     cout<<"dati n= ";
  147.     cin>>n;
  148.     while(n!=-1) {
  149.  
  150.         /* if(n%2==0) {
  151.              pare.adaugare(n);
  152.          } else {
  153.              impare.adaugare(n);
  154.          }*/
  155.         impare.adaugare(n);
  156.         cout<<"dati n= ";
  157.         cin>>n;
  158.     };
  159.  
  160.     /*cout<<"Lista pare ";
  161.     pare.afisare();*/
  162.  
  163.     cout<<"Lista: ";
  164.     impare.afisare();
  165.  
  166. //cout<<" Elementele inserate sunt suma a 2 consecutive"<< endl;
  167. //impare.suma();
  168. //impare.afisare();
  169.  
  170. //cout<<"stergere pare  ";
  171. //impare.stergerePare();
  172. //impare.afisare();
  173.  
  174.  //   cout<<"inversare lista   ";
  175.     //impare.inversareLista();
  176.     //impare.afisare();
  177.     cout<<"Bubble sort:   ";
  178.     impare.bubble();
  179.     impare.afisare();
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement