Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*g++ -std=c++11 nazwa.cpp
- */
- #include <iostream>
- using namespace std;
- template <class T>
- class LISTA;
- template<class T>
- class EL {
- template <class>
- friend class LISTA;
- template <class U>
- friend ostream & operator<< (ostream &s1, LISTA<U> & o1);
- template <class U>
- friend istream & operator>> (istream &s1, LISTA<U> &o1);
- T wartosc;
- class EL<T> *next;
- EL <T> (){
- T wartosc;
- next=NULL;
- }
- EL <T> (const L <T> &o1){
- wartosc=o1.wartosc;
- next=o1.next;
- }
- };
- template<class T>
- class LISTA {
- class EL<T> *head;
- template <class U>
- friend ostream & operator<< (ostream &s1, LISTA<U> & o1);
- template <class U>
- friend istream & operator>> (istream &s1, LISTA<U> &o1);
- public:
- LISTA<T>(){
- head=NULL;
- }
- LISTA<T>(class LISTA &o1){
- if(o1.head==NULL) return;
- auto *nowy = new EL<T>;
- nowy->wartosc=o1.head->wartosc;
- EL<T> *temp=o1.head;
- EL<T> *prev=nowy;
- head=nowy;
- temp=temp->next;
- while (temp!=NULL){
- EL<T> *elemencik=new EL<T>;
- prev->next=elemencik;
- prev=elemencik;
- temp=temp->next;
- }
- }
- ~LISTA<T>(){
- while(head!=NULL){
- class EL<T> *toDel;
- toDel=head;
- head=head->next;
- delete toDel;
- }
- }
- LISTA<T> operator =(const class LISTA<T> &o1){
- if (o1.head==NULL) return *this;
- auto *nowy = new EL<T>;
- nowy->wartosc=o1.head->wartosc;
- EL<T> *temp=o1.head;
- EL<T> *prev=nowy;
- head=nowy;
- temp=temp->next;
- while(temp!=NULL){
- auto *elemencik=new EL<T>;
- elemencik->wartosc=temp->wartosc;
- prev->next=elemencik;
- prev=elemencik;
- temp=temp->next;
- }
- prev->next=NULL;
- return *this;
- }
- LISTA<T> & operator + (LISTA<T> &o1){
- static LISTA<T> temp;
- if (this->head==NULL && o1.head==NULL)
- return temp;
- EL<T> *current = new EL<T>;
- EL<T> *node;
- EL<T> *i = this->head;
- EL<T> *j = o1.head;
- current->wartosc=i->wartosc;
- temp.head=current;
- while (i->next!=NULL){
- node=new EL<T>;
- current->next=node;
- i=i->next;
- node->wartosc=i->wartosc;
- current=node;
- }
- node=new EL<T>;
- node->wartosc=j->wartosc;
- current->next=node;
- while (j!=NULL){
- node=new EL<T>;
- current->next=node;
- node->wartosc=j->wartosc;
- current=node;
- j=j->next;
- }
- return temp;
- }
- bool operator == (const class LISTA<T> &o1){
- EL<T> *temp1=this->head;
- EL<T> *temp2=o1.head;
- int flaga=0;
- while(true){
- if (temp1==NULL) flaga+=1;
- if (temp2==NULL) flaga+=1;
- if (flaga==1) return false;
- else if (flaga==2) return true;
- if (temp1->wartosc!=temp2->wartosc) return false;
- temp1=temp1->next;
- temp2=temp2->next;
- }
- }
- bool operator != (const class LISTA<T> &o1){
- EL<T> *temp1=this->head;
- EL<T> *temp2=o1.head;
- int flaga=0;
- while (true){
- if (temp1==NULL) flaga+=1;
- if (temp2==NULL) flaga+=1;
- if (flaga==1) return true;
- if (flaga==2) return false;
- if(temp1->wartosc==temp2->wartosc) return false;
- }
- }
- };
- template<class T>
- ostream &operator<< (ostream &s1, LISTA<T> &o1){
- EL<T> *temp=o1.head;
- while(temp!=NULL){
- s1 << temp->wartosc<<" ";
- temp=temp->next;
- }
- return s1;
- }
- template<class T>
- istream &operator>> (istream &s1, LISTA<T> &o1){
- auto *element = new EL<T>;
- s1 >> element->wartosc;
- if(o1.head==NULL) {
- o1.head = element;
- o1.head->next=NULL;
- return s1;
- }
- EL<T> *n=o1.head;
- while(n->next!=NULL){
- n = n->next;
- }
- n->next = element;
- element->next = NULL;
- return s1;
- }
- int main (){
- LISTA<int> zm1,zm2,zm3;
- cout<<"Wpisz 3 elementy L1"<<endl;
- cin>>zm1>>zm1>>zm1;
- zm2=zm1;
- cout<<"L2 po ="<<endl;
- cout<<zm2<<endl;
- if(zm1==zm2) cout<<"Tak, L1==L2"<<endl;
- else cout<<"Nie, L1!=L2"<<endl;
- if(zm1!=zm2) cout<<"Tak2, L1!=L2"<<endl;
- else cout<<"Nie2, L1==L2"<<endl;
- cout<<"Wpisz kolejny element L1"<<endl;
- cin>>zm1;
- zm3=zm1+zm2;
- cout<<"L3 po L3=L1+L2"<<endl;
- cout<<zm3<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement