C++ problem

Hello,i'm kinda new here and i had to make a double linked list ( i think thats the english name) in C++. Adding addition,substraction,inseration,finding a key and deleting it etc.
I'm really stuck at adding the + operator to work. I have to do it ONLY with classes (no struct). Here's what i've done.

#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...I get a "DON'T SEND" error every time. Some values work ,some don't. Sometimes it stops at the 2nd or 3rd element at multC.afisare(), and just appears don't send screen. Or it finishes and after pressing any key it sends don't send again. at the last system("PAUSE").The other functions work. Only + , - , * , / ...gets me errors.
Sorry for the bad english.
Last edited on
I've only added the main,the + function and the main class forms.


You say this, but for the code to compile and run clearly Lista::creare, Lista::insert_front and Lista::afisare must be defined. Where's that code?

Thats the whole code, without the "\","*","-". Sorry for posting something so big.I'm not sure if destructor is ok..I've just learned how to use classes today from this site.
Last edited on
You need to define an operator= and copy constructor for Lista.



Last edited on
I tried this but didn't work. (with or without return) Or how should the code be?
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;
        
}
Like I said, you need both operator= and a copy constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Lista& Lista::operator= (const Lista & a)
{
	while(first!=NULL)
	{
		delete_first();
	}

	nod* d = a.first ;
	while(d!=NULL)
	{
		insert_front(d->x);
		d=d->next;
	}

	return *this ;        
}

Lista::Lista(const Lista & a) : first(0), last(0)
{
	operator=(a) ;
}


Also, in delete_first, you don't handle the case correctly where first points to the only nod in the list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Lista::delete_first()
{
	if(isEmpty())
	{
		cout<<"Eroare.Lista este goala."<<endl;             
	}
	else
	{
		nod * c = first ;
		first = first->next ;
		delete c ;

		if ( first == 0 )
			last = 0 ;
		else
			first->back = 0 ;

//		nod* c;
//		c=first;
//		first=first->next;
//		first->back=NULL;    
//		delete c;
	}
}


Presumably you make the same mistake in delete_last.
Last edited on
in case you didn't knew about the return *this ; at the end of the operator= () ... look it up ... it's very important for overloading the assignment operator ...
Thank you for pointing that out, i fixed delete_last and delete_first. But i'm stil stucked. With your code if i put
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Lista::Lista(const Lista & a) : first(0), last(0)
{
	operator=(a) ;
}

Lista Lista::operator= (const Lista & a)
{
	while(first!=NULL)
	{
		delete_first();
	}
	cout<<"3"<<endl
	afisare();

	nod* d = a.first ;
	while(d!=NULL)
	{
		insert_front(d->x);
		d=d->next;
	}

	return (*this) ;        
}


I get a infinite loop, telling me "33333333333333333333333333333333333333333...".
So it repeats the whole function (not the while loop). If i delete the constructor, it returns for exemple 3 0..and error (for MultA={1,2,3) and MultB={3,2,1} ). This code is for "MultA = MultB" . So it only returns first value correct.
@TwoOfDiamons Yea i knew about the *this pointer. tried it out but still getting the same error.

Also ty all for trying to help me, currently i'm reading some book, hopefully i'll find some answers there.
One quick question thou. I don't get it what that constructor does.
...:first(0) last(0)  and operator=(a);

A link where i should read would be greatful. Ty.
Last edited on
Oh nevermind i fixed it. I put :
Lista& Lista::operator= (const Lista & a)

Thank you all. Still confused with the constructor tho.

Edit: Now it works even without the copy constructor;
Last edited on
Topic archived. No new replies allowed.