ewa_tabor

Lista JIMP2

Mar 25th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. /* Klasa lista
  2. 1.obiekt to lista
  3. 2. << wypisuje wszystkie elementy, >> dodaje na koniec listy
  4. 3.konstr :) , dekonstr :) (nie rekurencyjnie bo zlozonosc niefajna), konstr kopiujacy :)
  5. 4. operatoty +, =, ==, !=
  6. */
  7.  
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. class LISTA;
  13.  
  14. class EL {
  15.     friend class LISTA;
  16.     friend ostream & operator<< (ostream &s1, LISTA & o1);
  17.     friend istream & operator>> (istream &s1, LISTA &o1);
  18.     int wartosc;
  19.     class EL *next;
  20. };
  21.  
  22. class LISTA {
  23.     class EL *head;
  24.     public:
  25.     LISTA(){
  26.         head=NULL;
  27.     }
  28.     LISTA(class LISTA &o1){
  29.         if(o1.head==NULL) return;
  30.         auto *nowy = new EL;
  31.         nowy->wartosc=o1.head->wartosc;
  32.         EL *temp=o1.head;
  33.         EL *prev=nowy;
  34.         head=nowy;
  35.         temp=temp->next;
  36.         while (temp!=NULL){
  37.             EL *elemencik=new EL;
  38.             prev->next=elemencik;
  39.             prev=elemencik;
  40.             temp=temp->next;
  41.         }
  42.     }
  43.     ~LISTA(){
  44.         while(head!=NULL){
  45.             class EL *toDel;
  46.             toDel=head;
  47.             head=head->next;
  48.             delete toDel;
  49.         }
  50.     }  
  51.     friend ostream & operator<< (ostream &s1, LISTA & o1);
  52.     friend istream & operator>> (istream &s1, LISTA &o1);
  53.    
  54.     LISTA operator =(const class LISTA &o1){
  55.         if (o1.head==NULL) return *this;
  56.         auto *nowy = new EL;
  57.         nowy->wartosc=o1.head->wartosc;
  58.         EL *temp=o1.head;
  59.         EL *prev=nowy;
  60.         head=nowy;
  61.         temp=temp->next;
  62.         while(temp!=NULL){
  63.             auto *elemencik=new EL;
  64.             elemencik->wartosc=temp->wartosc;
  65.             prev->next=elemencik;
  66.             prev=elemencik;
  67.             temp=temp->next;
  68.         }
  69.         prev->next=NULL;
  70.         return *this;
  71.     }
  72.     LISTA & operator + (LISTA &o1){
  73.       static LISTA temp;
  74.       if (this->head==NULL && o1.head==NULL)
  75.         return temp;
  76.       EL *current = new EL;
  77.       EL *node;
  78.       EL *i = this->head;
  79.       EL *j = o1.head;
  80.       current->wartosc=i->wartosc;
  81.       temp.head=current;
  82.       while (i->next!=NULL){
  83.           node=new EL;
  84.           current->next=node;
  85.           i=i->next;
  86.           node->wartosc=i->wartosc;
  87.           current=node;
  88.       }
  89.       node=new EL;
  90.       node->wartosc=j->wartosc;
  91.       current->next=node;
  92.       while (j!=NULL){
  93.           node=new EL;
  94.           current->next=node;
  95.           node->wartosc=j->wartosc;
  96.           current=node;
  97.           j=j->next;
  98.       }
  99.       return temp;
  100.     }
  101.    
  102.     bool operator == (const class LISTA &o1){
  103.         EL *temp1=this->head;
  104.         EL *temp2=o1.head;
  105.         int flaga=0;
  106.         while(true){
  107.             if (temp1==NULL) flaga+=1;
  108.             if (temp2==NULL) flaga+=1;
  109.             if (flaga==1) return false;
  110.             else if (flaga==2) return true;
  111.             if (temp1->wartosc!=temp2->wartosc) return false;
  112.             temp1=temp1->next;
  113.             temp2=temp2->next;
  114.         }
  115.     }
  116.     bool operator != (const class LISTA &o1){
  117.         EL *temp1=this->head;
  118.         EL *temp2=o1.head;
  119.         int flaga=0;
  120.         while (true){
  121.             if (temp1==NULL) flaga+=1;
  122.             if (temp2==NULL) flaga+=1;
  123.             if (flaga==1) return true;
  124.             if (flaga==2) return false;
  125.             if(temp1->wartosc==temp2->wartosc) return false;
  126.         }
  127.     }
  128.  
  129. };
  130.  
  131. ostream &operator<< (ostream &s1, LISTA &o1){
  132.     EL *temp=o1.head;
  133.     while(temp!=NULL){
  134.         s1 << temp->wartosc<<" ";
  135.         temp=temp->next;
  136.     }
  137.     return s1;
  138. }
  139.    
  140.  
  141. istream &operator>> (istream &s1, LISTA &o1){
  142.     auto *element = new EL;
  143.     s1 >> element->wartosc;
  144.     if(o1.head==NULL) {
  145.         o1.head = element;
  146.         o1.head->next=NULL;
  147.         return s1;
  148.     }
  149.     EL *n=o1.head;
  150.     while(n->next!=NULL){
  151.         n = n->next;
  152.     }
  153.     n->next = element;
  154.     element->next = NULL;
  155.     return s1;
  156. }
  157.  
  158.  
  159. int main (){
  160.     LISTA zm1,zm2,zm3;
  161.     cin>>zm1>>zm1>>zm1;
  162.     zm2=zm1;
  163.     cout<<zm2<<endl;
  164.     if(zm1==zm2) cout<<"Tak"<<endl;
  165.     else cout<<"Nie"<<endl;
  166.     if(zm1!=zm2) cout<<"Tak2"<<endl;
  167.     else cout<<"Nie2"<<endl;
  168.     cin>>zm1;
  169.     zm3=zm1+zm2;
  170.     cout<<zm3;
  171.  
  172.  
  173.  
  174.     return 0;
  175. }
Add Comment
Please, Sign In to add comment