0

I have been trying for quite a while to figure out how to get pointers for my nodes to merge two linked lists. I have to do it with the following function definition (NB I have found plenty of answers when the pointers are arguments to a member function but in my case I have to do it with a reference. Any pointers :) would be appreciated. NB I know that my code is not yet fully developed but I really need to get pointers to both OrderedList objects to make it work. ie if I can get two pointers to the _head of each ordered link list I think I can generate the code.

Any help to get a valid reference for curr and mov would be appreciated.

OrderedList operator+(OrderedList&second) // merges two OrderedLists
{
    int i=1;
    int j=1;

    Node* prev = NULL;
    Node* curr = this->_head;
    Node* mov = second._head;

    while (curr!=NULL and mov!=NULL)
    {

        if (this->operator [](i)<= second->operator [](j) or mov->next == NULL)
        {
            i++;
        }else{
            prev->next = mov;
            mov = mov->next;
            mov = curr;
        }
    }
    return OrderedList;
}

so this is for an assignment so I want to learn how to do it correctly. I am not just interested in answers but mainly understanding. I am new to c++. So basically we have been given a header file and have to make all the member and friend functions work. I am stuck on how to make this operator function work. So the header file is:

#ifndef ORDEREDLIST_H_
#define ORDEREDLIST_H_

#include <iostream>
using namespace std;
class Node
{
public:
double item; // data item
Node *next; // pointer to next node
};
class OrderedList
{

// friend operator functions
friend ostream &operator<<(ostream &, const OrderedList &);
friend istream &operator>>(istream &, OrderedList &);

public:
// constructors
OrderedList():_head(NULL),_size(0) {};
OrderedList(const OrderedList &); // copy constructor

// destructor
~OrderedList();
// operator functions
OrderedList operator+(OrderedList&); // merges two OrderedLists
double operator[](int) const; // subscript operator returns rvalue
const OrderedList &operator=(const OrderedList &); // assignment

void insert(double); // inserts an item
bool remove(int); // remove i th item
void pop(); // pop the first item
//void printlist(Node*);

private:
Node *_head;
int _size;
};



#endif /* ORDEREDLIST_H_ */

Can I only get a pointer if the overloaded operator is a friend of OrderedList or if it is a member function? That is what I am seeing but I am very new to C++.

0 Answers0