Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- // OBIEKT LISTA DONE
- // operator wejscia dodaje 1 element, wyjscia wypisuje wszystkie el DONE
- // konstruktor, destruktor, konstruktor kopiujący DONE
- // + do listy dopisuje na końcu drógą DONE
- // =, ==, != DONE
- class List;
- class Element;
- template<class T>
- class Element{
- public:
- T war;
- class Element *next;
- Element(){
- war=T;
- next=NULL;
- }
- };
- class List{
- friend ostream & operator << (ostream & s1, List & o1);
- friend istream & operator >> (istream & s1, List & o1);
- class Element *pierwszy;
- public:
- List(){
- pierwszy=NULL;
- }
- List (class List &o1){
- if (o1.pierwszy==NULL){
- pierwszy=NULL;
- }
- else{
- class Element *nowy = new Element;
- nowy->war=o1.pierwszy->war;
- Element *i =o1.pierwszy;
- Element *poprz = nowy;
- pierwszy=nowy;
- i=i->next;
- while (i!=NULL){
- class Element *elem = new Element;
- elem->war=i->war;
- poprz->next=elem;
- poprz=elem;
- i=i->next;
- }
- poprz->next=NULL;
- }
- }
- ~List(){
- while (pierwszy!=NULL){
- if (pierwszy->next != NULL){
- class Element *nowy=pierwszy->next;
- delete pierwszy;
- pierwszy = nowy;
- }
- else{
- delete pierwszy;
- pierwszy = NULL;
- }
- }
- }
- List & operator = (List &o1)
- {
- if (this==&o1)
- return *this;
- else{
- while (pierwszy!=NULL){
- if (pierwszy->next != NULL){
- class Element *nowy=pierwszy->next;
- delete pierwszy;
- pierwszy = nowy;
- }
- else{
- delete pierwszy;
- pierwszy = NULL;
- }
- }
- }
- if (o1.pierwszy==NULL){
- return *this;
- }
- else{
- Element *current = new Element;
- Element *node;
- Element *wsk = o1.pierwszy;
- current->war=wsk->war;
- this->pierwszy=current;
- while (wsk->next!=NULL)
- {
- node = new Element;
- current->next=node;
- wsk=wsk->next;
- node->war=wsk->war;
- current=node;
- }
- return *this;
- }
- }
- friend List & operator + (List &o1, List &o2){
- static List temp;
- if (o1.pierwszy==NULL && o2.pierwszy==NULL){
- return temp;
- }
- else if (o1.pierwszy==NULL && o2.pierwszy != NULL){
- temp=o2;
- return temp;
- }
- else if (o1.pierwszy!=NULL && o2.pierwszy==NULL){
- temp=o1;
- return temp;
- }
- Element *current = new Element;
- Element *node;
- Element *i = o1.pierwszy;
- Element *j = o2.pierwszy;
- current->war=i->war;
- temp.pierwszy=current;
- while (i->next!=NULL)
- {
- node=new Element;
- current->next=node;
- i=i->next;
- node->war=i->war;
- current=node;
- }
- node=new Element;
- node->war=j->war;
- current->next=node;
- while (j!=NULL)
- {
- node=new Element;
- current->next=node;
- node->war=j->war;
- current=node;
- j=j->next;
- }
- return temp;
- }
- int operator == (List &o1){
- if (this->pierwszy ==NULL && o1.pierwszy==NULL){
- return true;
- }
- if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
- return false;
- }
- if (o1.pierwszy!=NULL){
- Element *head=o1.pierwszy;
- Element *headthis=this->pierwszy;
- while (head->next!=NULL)
- {
- if (head->war!=headthis->war)
- return false;
- head=head->next;
- headthis=headthis->next;
- }
- return true;
- }
- else return false;
- }
- bool operator != (List &o1){
- if (this->pierwszy==NULL && o1.pierwszy==NULL){
- return false;
- }
- else if (this->pierwszy ==NULL && o1.pierwszy!=NULL){
- return true;
- }
- else if (this->pierwszy !=NULL && o1.pierwszy==NULL){
- return true;
- }
- else{
- bool wynik;
- Element *head=o1.pierwszy;
- Element *headthis=this->pierwszy;
- while (head->next!=NULL && headthis->next!=NULL)
- {
- if (head->war!=headthis->war)
- wynik=true;
- else{
- wynik=false;
- break;
- }
- head=head->next;
- headthis=headthis->next;
- }
- if (head->next==NULL && headthis ->next!=NULL){
- wynik=false;
- }
- return wynik;
- }
- }
- };
- ostream & operator<< (ostream & s1, class List & o1){
- Element *n = o1.pierwszy;
- while(n){
- s1 << n->war << ", " ;
- n = n->next;
- }
- cout << endl;
- return s1;
- }
- istream & operator >>(istream &s1, class List &o1){
- class Element *element = new Element;
- cout <<"Podaj wartość elementu listy ";
- s1 >> element->war;
- cout <<endl;
- if (o1.pierwszy==NULL){
- o1.pierwszy=element;
- o1.pierwszy->next=NULL;
- }
- else{
- class Element *wsk =o1.pierwszy;
- while(wsk->next != NULL){
- wsk=wsk->next;
- }
- wsk->next = element;
- element->next = NULL;
- }
- return s1;
- }
- int main()
- {
- List o1;
- List o2;
- List o4;
- int rozm1, rozm2;
- cout<<"podaj rozmiar 1 listy: "<<endl;
- cin>>rozm1;
- for (int i=0; i<rozm1; i++)
- cin>>o1;
- cout<<"podaj rozmiar 2 listy: "<<endl;
- cin>>rozm2;
- for (int i=0; i<rozm2; i++)
- cin>>o2;
- cout<<"pierwsza lista: ";
- cout<<o1;
- cout<<"druga lista: ";
- cout<<o2;
- cout<<"lista 3 - kopia listy 1: ";
- List o3 (o1);
- cout<<o3;
- cout<<"dodawanie listy 2 do listy 1: ";
- cout<<o1+o2;
- cout<<"dodawanie listy 1 do listy 2: ";
- cout<<o2+o1;
- if (o1==o3)
- cout<<"działa 1"<<endl;
- if(o1!=o2)
- cout<<"działa 2"<<endl;
- o4=o1+o2;
- cout<<"lista 4=1+2: ";
- cout<<o4;
- o1.~List();
- return 0;
- }
- //zaprzyjaźnić wzorzec klasy we wzorcu klasy sprawdzić jak to jest w necie
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement