简体   繁体   中英

Intersection and Union of two sorted lists (return type)

This code is for finding the intersection and union of two sorted lists. The sorted list in inherited from a list class with all the basic functions. The main question is what is the return type of the function. Is it a pointer to a list or the list itself? How would i display the contents of that "pointer".

template <typename Object>
class sorted_list : public List<Object>{
    friend sorted_list<Object>*& slUnion( const sorted_list<Object>& list1, const sorted_list<Object> & list2){
        auto i=list1.begin();
        auto j=list2.begin();
        sorted_list<Object> un;
        static sorted_list<Object>* newlist=&un;
        while(i!=list1.end() && j!=list2.end()){
            if(*i<*j){
                un.push_back(*i);
                i++;
            }
            else if(*i>*j){
                un.push_back(*j);
                j++;
            }
            else{ //if equal
                un.push_back(*i);
                i++; j++;
            }
        }
        while(i!=list1.end())
            un.push_back(*i++);
        while(j!=list2.end())
            un.push_back(*j++);
        return newlist;
    }
};

When the program runs, the "un" in main points to NULL.

int main(){
    sorted_list<int> l1;
    int i=1;
    while(i<10){
        l1.push_back(i++);
    }
    sorted_list<int>l2;
    int j=1;
    while(j<10){
        l2.push_back(j);
        j+=2;
    }
    sorted_list<int> *un = slUnion(l1,l2);
}

You should typically return by value, ie sorted_list<Object> . Newer versions of cpp guarantee you that they will not actually make a copy.

What you are doing right now is wrong, because it has undefined behavior. You are using un , which is on the function stack, and return a pointer to it. By the time the function returns un has gone out of scope and the memory location can have been reused. Just completely remove the newlist pointer and return un instead.

You also seem to be confused about classes, methods and functions. As it is, your method does not have to be inside a class, or, since it does not seem to use class state, it can be static, if inside a class. It also does not seem like it would have to be a friend. If you wanted to write this as a member-function, it would look sth like this:

sorted_list<Object>& unionWith(const sorted_list<Object>& rhs) {
  // merge this and rhs w deduplication into temp, then swap temp with this
  ...

  return *this;
}

I think what is likely your problem, is that you don't assign &un to newlist , but that you initialize newlist with &un . Initialization is only performed once for a function-static variable, so future iterations of your method just skip that line and the pointer points to where the original version of un was. Try putting the assignment on a different line. That should fix your immediate issue, but the solution with the static pointer is still really bad, because the pointer is shared by all instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM