Advertisement
ewa_tabor

Lista_wzorzec

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