Thread: returning a linked list from a function

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    returning a linked list from a function

    Hi all,
    I am trying to return a linked list from a function. The code compiles, but chokes when I try to use the function getEquation(). Assuming returning a link list is the problem, how do I rectify this?

    PHP Code:
    /*
     *  eq.h
     *
     *  Created by Jeffery Shipman on 1/19/06.
     *  class of equations accepting polys
     *
     *  poly header must load first
     */

    class eq
    {
        public:
            
    eq(string n);  //default constructor
            
    eq(const eq &);  //copy constructor
            
    void add(poly *); //add poly to the equation
            
    void show();  //display
            
    void setName(string n){name n;} //set name of equation
            
    string getName() const;//returns name of equation
            
    list <polygetEquation() const; //returns equation
            
    void eq_sum (eq &); //produce sum of two equations
            
    void simplify();  //sum like elements in a single list
            
    bool operator==(const eq &) const;  //compare equal
            
    eq operator=( const eq & );  //assign lists
            
        
    private:
            
    string name;
            list <
    polyequation;
        
    };

    eq::eq(string n)
        {
            
    name n;
            list <
    poly>::iterator itr;
            
    itr equation.begin();
        }

    eq::eq(const eq &e)
        {
            
    poly *element;
            
    name e.name;
            list <
    poly>::const_iterator const_itr;
            
    const_itr equation.begin();
            
            while(
    const_itr!=equation.end())
            {
                    
    element = new poly(const_itr->getCoeff (), const_itr->getExp ());
                    
    add(element);
                    
    const_itr++;
            }
            
        }

    void eq::add(poly *element)
        {
            
    //p is pointing at a poly
            //push_front *p follows the pointer to
            //that poly..and pushes him/her to 
            //the equation
            
    equation.push_back(*element);
        }

        
    void eq::show()
        {
            
    cout<<"polys currently in equation" << endl;
            list <
    poly>::iterator itr;
            
    itr equation.begin();
            while(
    itr!=equation.end())
                {
                if (
    itr->getCoeff()!=0)
                    {
                        
    itr->display();
                    }
                
    itr++;
                }
        }

    string eq::getName() const
        {
            return 
    name;
        }
        
    list <
    polyeq::getEquation() const
        {
            return 
    equation;
        }
        
    void eq::simplify()
        {
            list <
    poly>::iterator itr;
            list <
    poly>::iterator itr2;
            
    itr equation.begin();
            
    itr2 equation.begin();
            
    itr2++;
            
    //cout << "while(p!=equation.end() && itr!=itr2)" << endl; 
            
    while(itr!=equation.end() && itr!=itr2)
                {
                    
    //cout << "  while(itr2!=equation.end() &&  itr!=itr2)" << endl; 
                    
    while(itr2!=equation.end() &&  itr!=itr2)
                    {
                        
    //cout << "     " ;
                        //itr->display();
                        //cout << " == " ;
                        //itr2->display() ;
                        //cout << endl;
                        
    if (itr->getExp() == itr2->getExp())
                        {
                            
    itr->setCoeff(itr->getCoeff() + itr2->getCoeff());  //add the coeff
                            
    equation.erase(itr2);  //erase the match from the list
                            //the following line of code are not necessary in gcc 4
                            //but are in gcc 3
                            //iterator pointing no where after erase
                            //assign iterator
                            
    itr2 itr;
                        }
                        
    itr2++;
                    }
                    
    //the following three lines of code are not necessary in gcc 4
                    //but are in gcc 3
                    
    itr++;
                    
    itr2 itr;
                    
    itr2++;
                }
        }

    void eq::eq_sum (eq &second)
        {
            list <
    poly>::iterator itr;
            list <
    poly>::iterator itr2;
            
    itr equation.begin();
            
    itr2 second.getEquation().begin();
            
            
    //simply two given equations
            
    simplify();
            
    show();
            
    cout << endl << endl;
            
    second.simplify();
            
    second.show();
            
    cout << endl << endl;
            
            
    int test 0;
            
    poly *element;
            
    eq sum ("sum");
            
            
    cout <<"after creation of sum" << endl << endl;
            while (
    itr != equation.end())
                {
                    
    cout <<" inside first while loop"  << endl;
                    
    test 0;
                    
    itr2 second.getEquation().begin();
                    while (
    itr2 != second.getEquation().end())
                        {
                            
    cout << "     ";
                            
    itr->display();
                            
    cout << " == " ;
                            
    itr2->display();
                            
    cout << endl;
                            
    //combine similar elements then add to finally equation
                            
    if (itr->getExp() == itr2->getExp())
                                {
                                    
    element = new poly(itr->getCoeff() + itr2->getCoeff(), 
                                                                
    itr->getExp() + itr2->getExp());
                                    
    sum.add(element);
                                    
    test++;
                                }
                            
    itr2++;
                         }
                    
    //if there are no matches add element to sum
                    
    if (test == 0)
                        {
                            
    element = new poly(itr->getCoeff(), itr->getExp());
                            
    sum.add(element);
                        }    
                    
    itr++;
                }
            
    //re-initialize values for new looping
            
    itr =  equation.begin();
            
    itr2 =  second.getEquation().begin();
            while (
    itr2 != second.getEquation().end())
                {
                    
    test 0;
                    
    itr =  equation.begin();
                    while (
    itr != getEquation().end())
                        {
                            if (
    itr2->getExp() == itr->getExp())
                                {
                                    
    test++;
                                }
                            
    itr++;
                        }
                    if (
    test == 0)
                        {
                            
    element = new poly(itr2->getCoeff(), itr2->getExp());
                            
    sum.add(element);
                        }
                    
    itr2++;
                }
            
    sum.show();
        }
    bool eq::operator==(const eq &right) const  //compare equal
        
    {
                list <
    poly>::const_iterator itr;
                list <
    poly>::const_iterator itr2;
                
    itr =  equation.begin();
                
    itr2 right.equation.begin();
                
                if (
    equation.size () == right.equation.size ())
                    {
                        while(
    itr!=equation.end())
                        {
                          if (!((
    itr->getCoeff() == itr2->getCoeff()) && (itr->getExp() == itr2->getExp())) )
                            return 
    false;
                          else
                            {
                                
    itr++;
                                
    itr2++;
                            }
                        }
                        return 
    true;
                    }
                else 
                    return 
    false;
         }
         
    eq eq::operator=( const eq &right )
        {
            
    poly *element;
            list <
    poly>::const_iterator itr;
            list <
    poly>::const_iterator itr2;
            
    itr =  equation.begin();
            
    itr2 right.equation.begin();
        
          if (
    this != &right)
          {
            
    //cout << "pass the first if" << endl;
            //clear target list
            
    while(itr!=equation.end())
            {
                
    //cout << "  inside the clear target while" << endl;
                
    equation.pop_front ();
            }
            
    //cout << "pass the clear target while" << endl;
            //cout << "size of right equation  "  << right.equation.size() << endl;
            
    while(itr2!=right.equation.end())
            {
                
    //cout << "  inside the add while" << endl;
                
    element = new poly(itr2->getCoeff (), itr2->getExp ());
                
    add(element);
                
    itr2++;
            }
          }
          return *
    this;
        } 

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your list holds poly objects, so you should not be using new at all in your program. You are leaking memory every time you use new to create an object and then add it to the list without deleting the pointer. Remember that in C++ lists, a copy of the object is stored in the list.

    Note that you don't need a copy constructor or an operator= since the eq class can copy itself safely. However, besides potential problems arising from using new unnecessarily, if the class poly isn't copyable, that might be what is causing your problem. If getting rid of new doesn't solve it, post the poly class declaration.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Hi,
    I have taken care of the pointers by adding a deconstructor which performs delete. I disagree with you about the copy constructor and operator=. If I do not define these elements when I use the them, I will get a shallow copy instead of a "real" copy. I am still receiving the error with the added correction.

    PHP Code:
    /*
     *  eq.h
     *
     *  Created by Jeffery Shipman on 1/19/06.
     *  class of equations accepting polys
     *
     *  poly header must load first
     */

    class eq
    {
        public:
            
    eq(string n);  //default constructor
            
    eq(const eq &);  //copy constructor
            
    ~eq(); //destructor
            
    void add(poly *); //add poly to the equation
            
    void show();  //display
            
    void setName(string n){name n;} //set name of equation
            
    string getName() const;//returns name of equation
            
    list <polygetEquation() const; //returns equation
            
    void eq_sum (eq &); //produce sum of two equations
            
    void simplify();  //sum like elements in a single list
            
    bool operator==(const eq &) const;  //compare equal
            
    eq operator=( const eq & );  //assign lists
            
        
    private:
            
    string name;
            list <
    polyequation;
            
    poly *element;
        
    };

    eq::eq(string n)
        {
            
    name n;
            list <
    poly>::iterator itr;
            
    itr equation.begin();
        }

    eq::eq(const eq &e)
        {
            
    //poly *element;
            
    name e.name;
            list <
    poly>::const_iterator const_itr;
            
    const_itr equation.begin();
            
            while(
    const_itr!=equation.end())
            {
                    
    element = new poly(const_itr->getCoeff (), const_itr->getExp ());
                    
    add(element);
                    
    const_itr++;
            }
            
        }
        
    eq::~eq()
        {
            
    delete element;
        }
        
    void eq::add(poly *element)
        {
            
    //p is pointing at a poly
            //push_front *p follows the pointer to
            //that poly..and pushes him/her to 
            //the equation
            
    equation.push_back(*element);
        }

        
    void eq::show()
        {
            
    cout<<"polys currently in equation" << endl;
            list <
    poly>::iterator itr;
            
    itr equation.begin();
            while(
    itr!=equation.end())
                {
                if (
    itr->getCoeff()!=0)
                    {
                        
    itr->display();
                    }
                
    itr++;
                }
        }

    string eq::getName() const
        {
            return 
    name;
        }
        
    list <
    polyeq::getEquation() const
        {
            return 
    equation;
        }
        
    void eq::simplify()
        {
            list <
    poly>::iterator itr;
            list <
    poly>::iterator itr2;
            
    itr equation.begin();
            
    itr2 equation.begin();
            
    itr2++;
            
    //cout << "while(p!=equation.end() && itr!=itr2)" << endl; 
            
    while(itr!=equation.end() && itr!=itr2)
                {
                    
    //cout << "  while(itr2!=equation.end() &&  itr!=itr2)" << endl; 
                    
    while(itr2!=equation.end() &&  itr!=itr2)
                    {
                        
    //cout << "     " ;
                        //itr->display();
                        //cout << " == " ;
                        //itr2->display() ;
                        //cout << endl;
                        
    if (itr->getExp() == itr2->getExp())
                        {
                            
    itr->setCoeff(itr->getCoeff() + itr2->getCoeff());  //add the coeff
                            
    equation.erase(itr2);  //erase the match from the list
                            //the following line of code are not necessary in gcc 4
                            //but are in gcc 3
                            //iterator pointing no where after erase
                            //assign iterator
                            
    itr2 itr;
                        }
                        
    itr2++;
                    }
                    
    //the following three lines of code are not necessary in gcc 4
                    //but are in gcc 3
                    
    itr++;
                    
    itr2 itr;
                    
    itr2++;
                }
        }

    void eq::eq_sum (eq &second)
        {
            list <
    poly>::iterator itr;
            list <
    poly>::iterator itr2;
            
    itr equation.begin();
            
    itr2 second.getEquation().begin();
            
            
    //simply two given equations
            
    simplify();
            
    show();
            
    cout << endl << endl;
            
    second.simplify();
            
    second.show();
            
    cout << endl << endl;
            
            
    int test 0;
            
    //poly *element;
            
    eq sum ("sum");
            
            
    cout <<"after creation of sum" << endl << endl;
            while (
    itr != equation.end())
                {
                    
    cout <<" inside first while loop"  << endl;
                    
    test 0;
                    
    itr2 second.getEquation().begin();
                    while (
    itr2 != second.getEquation().end())
                        {
                            
    cout << "     ";
                            
    itr->display();
                            
    cout << " == " ;
                            
    itr2->display();
                            
    cout << endl;
                            
    //combine similar elements then add to finally equation
                            
    if (itr->getExp() == itr2->getExp())
                                {
                                    
    element = new poly(itr->getCoeff() + itr2->getCoeff(), 
                                                                
    itr->getExp() + itr2->getExp());
                                    
    sum.add(element);
                                    
    test++;
                                }
                            
    itr2++;
                         }
                    
    //if there are no matches add element to sum
                    
    if (test == 0)
                        {
                            
    element = new poly(itr->getCoeff(), itr->getExp());
                            
    sum.add(element);
                        }    
                    
    itr++;
                }
            
    //re-initialize values for new looping
            
    itr =  equation.begin();
            
    itr2 =  second.getEquation().begin();
            while (
    itr2 != second.getEquation().end())
                {
                    
    test 0;
                    
    itr =  equation.begin();
                    while (
    itr != getEquation().end())
                        {
                            if (
    itr2->getExp() == itr->getExp())
                                {
                                    
    test++;
                                }
                            
    itr++;
                        }
                    if (
    test == 0)
                        {
                            
    element = new poly(itr2->getCoeff(), itr2->getExp());
                            
    sum.add(element);
                        }
                    
    itr2++;
                }
            
            
    sum.show();
        }
    bool eq::operator==(const eq &right) const  //compare equal
        
    {
                list <
    poly>::const_iterator itr;
                list <
    poly>::const_iterator itr2;
                
    itr =  equation.begin();
                
    itr2 right.equation.begin();
                
                if (
    equation.size () == right.equation.size ())
                    {
                        while(
    itr!=equation.end())
                        {
                          if (!((
    itr->getCoeff() == itr2->getCoeff()) && (itr->getExp() == itr2->getExp())) )
                            return 
    false;
                          else
                            {
                                
    itr++;
                                
    itr2++;
                            }
                        }
                        return 
    true;
                    }
                else 
                    return 
    false;
         }
         
    eq eq::operator=( const eq &right )
        {
            
    //poly *element;
            
    list <poly>::const_iterator itr;
            list <
    poly>::const_iterator itr2;
            
    itr =  equation.begin();
            
    itr2 right.equation.begin();
        
          if (
    this != &right)
          {
            
    //cout << "pass the first if" << endl;
            //clear target list
            
    while(itr!=equation.end())
            {
                
    //cout << "  inside the clear target while" << endl;
                
    equation.pop_front ();
            }
            
    //cout << "pass the clear target while" << endl;
            //cout << "size of right equation  "  << right.equation.size() << endl;
            
    while(itr2!=right.equation.end())
            {
                
    //cout << "  inside the add while" << endl;
                
    element = new poly(itr2->getCoeff (), itr2->getExp ());
                
    add(element);
                
    itr2++;
            }
          }
          
          return *
    this;
        } 
    Last edited by jlshipman; 01-23-2006 at 06:44 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I have taken care of the pointers by adding a deconstructor which performs delete.
    Unfortunately, adding a destructor won't work. It only deletes the last element added to the list. If you add more than one element, you will leak all but the last one.

    Your add method should take a poly object by value or by const reference, not by pointer. Then you should create your temporary element on the stack and pass it to add without using new at all (you don't need new anywhere in the posted code).

    >> I disagree with you about the copy constructor and operator=. If I do not define these elements when I use the them, I will get a shallow copy instead of a "real" copy.

    This is also not true. Assuming list and string are the C++ list and string classes, they will be copied correctly because they have their own copy constructor and copy assignment operator. Since those are the only two pieces of data in your class, your class will be copied correctly.

    Again, your error might be due to a shallow copy of poly, but you didn't post the poly class declaration. Also, your confusion might be due to a familiarity with Java. If you come from a Java background, please ignore what you learned about pointers and new in Java, it is very different in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list noob - function create new list
    By dukysta in forum C Programming
    Replies: 5
    Last Post: 07-06-2007, 08:16 AM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM