SHARE
TWEET

ASD Pb 16 liste medie

madalinaradu May 26th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Lista;
  5.  
  6. class Nod {
  7. private:
  8.     float info;
  9.     Nod *next;
  10. public:
  11.     Nod(float info = 0, Nod *next = NULL) {
  12.         this -> info = info;
  13.         this -> next = next;
  14.     }
  15.  
  16.     float getInfo() {
  17.         return info;
  18.     }
  19.  
  20.     friend class Lista;
  21. };
  22.  
  23. class Lista {
  24. private:
  25.     Nod* prim;
  26.     Nod* ultim;
  27.  
  28. public:
  29.     Lista();
  30.     ///void adaugaSfarsit(int info);
  31.     void adaugaInceput(float info);
  32.     void afisare();
  33.     void nodDiferenta();
  34. };
  35.  
  36. Lista::Lista() {
  37.     prim=ultim=NULL;
  38. }
  39.  
  40. void Lista::adaugaInceput(float info) {
  41.     Nod* p = new Nod(info);
  42.     if (prim == NULL) {
  43.         prim = ultim = p;
  44.     } else {
  45.         p->next =prim;
  46.         prim = p;
  47.     }
  48. }
  49.  
  50. void Lista::afisare() {
  51.     Nod *p = prim;
  52.     cout<<endl;
  53.     cout<<"[";
  54.     while(p != NULL) {
  55.         cout<<p->info<<"    ";
  56.         p= p->next;
  57.     }
  58.     cout<<"]"<<endl;
  59. }
  60. void Lista::nodDiferenta() {
  61.     if(prim!=NULL && prim->next!=NULL) {
  62.         Nod *q,*p;///parcurgem lista
  63.         q=prim;
  64.         p=q->next;
  65.         while(p!=NULL) {
  66.             Nod *temp;/// in el se calculeaza suma a 2 elem consecutive;
  67.             temp=new Nod;
  68.             temp->info=p->info - q->info;
  69.             temp->next=p;
  70.             q->next=temp;
  71.             q=p;///q sare la nodul p, peste cel nou creat
  72.             p=p->next;
  73.         }
  74.     }
  75. }
  76.  
  77. int main() {
  78.     Lista lista;
  79.     lista.adaugaInceput(1);
  80.     lista.adaugaInceput(2);
  81.     lista.adaugaInceput(5);
  82.     lista.adaugaInceput(8);
  83.     lista.afisare();
  84.     cout<<" Elementele inserate sunt diferenta a 2 consecutive"<< endl;
  85.     lista.nodDiferenta();
  86.     lista.afisare();
  87. }
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