#include <cstdlib> #include <iostream> using namespace std; class Lista; class nod{ nod* next; nod* back; public: float x; nod(float a){x=a;back=NULL;next=NULL;} nod(){} friend class Lista; }; class Lista{ public: nod* first; nod* last; Lista(){first=NULL;last=NULL;} //-constructor void creare(); //Creates the list bool isEmpty(){return (first==NULL);} //-Verifies if its empty void insert_front(float x); //-Inserts left (la inceputul listei) void insert_back(float x); //-Inserts right( la sfarsitul listei) void afisare(); // Shows the whole list from right to left void delete_last(); // deletes last element void delete_first(); //-deletes first element void gasire_cheie(float k); //- finds element with k value. void delete_cheie(nod* k); //- deletes element k Lista operator+ (Lista); ~Lista(); //-destructor }; Lista Lista::operator+ (Lista a) { Lista art; nod* c; nod* d; float temp; d=a.first; c=first; while(c!=NULL && d!=NULL) { temp=((c->x)+(d->x)); art.insert_front(temp); c=c->next; d=d->next; } art.afisare(); system("PAUSE"); return art; } void Lista::creare() { cout<<"How many elements do you want to add "<<endl; int n; float x; cin>>n; for(int i=1;i<=n;i++) { cout<<"Insert the next element: ("<<(n-i+1)<<" more elements to insert)"<<endl; cin>>x; insert_front(x); } } void Lista::delete_cheie(nod* k) { nod* c; c=k->back; c->next=k->next; c=k->next; c->back=k->back; delete k; } void Lista::gasire_cheie(float k) { nod* c; nod* p; int contur=0; c=first; while(c!=NULL) { if(k==c->x) { contur++; p=c; } c=c->next; } if(contur>1) { cout<<"In the list there are "<<contur<<" elements with valor "<<k<<"."<<endl; cout<<"Which element do you want to delete?(Choose 1,2,3...etc)"<<endl; int choose; cin>>choose; while(choose<1 && choose>contur) { cout<<"Valoarea data nu exista!Reintroduceti!"<<endl; cin>>choose; } c=first; while(c!=NULL) { if(k==c->x) { if(choose==1) { if(c!=last && c!=first) {delete_cheie(c);} else { if(c==last) { delete_last(); } else { delete_first(); } } break; } choose--; } c=c->next; } } if(contur==1) { delete_cheie(p); } if(contur==0) { cout<<"The searched element doesn't exist"<<endl; } } void Lista::afisare() { nod* c; c=first; while(c!=NULL) { cout<<c->x<<" "; c=c->next; } } void Lista::insert_back(float x) { nod* a=new nod(x); if(isEmpty()) { first=a; last=a; } else { first->back=a; a->next=first; first=a; } } void Lista::insert_front(float x) { nod* a=new nod(x); if(isEmpty()) { first=a; last=a; } else { last->next=a; a->back=last; last=a; } } Lista::~Lista() { nod* c; while(first!=NULL) { c=first; first=first->next; delete c; } } void Lista::delete_first() { if(isEmpty()) { cout<<"Eroare.Lista este goala."<<endl; } else { nod* c; c=first; first=first->next; first->back=NULL; delete c; } } void Lista::delete_last() { if(isEmpty()) { cout<<"Error.List is empty"<<endl; } else { nod* c; c=last; last=last->back; last->next=NULL; delete c; } } int main() { cout<<"First list:"<<endl; Lista multA; multA.creare(); Lista multB; Lista multC; cout<<"Second List:"<<endl; multB.creare(); multC = multA + multB; multC.afisare(); system("PAUSE"); return 0; } |
I've only added the main,the + function and the main class forms. |
Lista Lista::operator= (Lista a) { nod* d; d=a.first; while(first!=NULL) { delete_first(); } while(d!=NULL) { insert_front(d->x); d=d->next; } return a; } |
|
|
|
|
return *this ;
at the end of the operator= () ... look it up ... it's very important for overloading the assignment operator ...
|
|
...:first(0) last(0) and operator=(a); |
Lista& Lista::operator= (const Lista & a) |