0

Before you read this, I am new to c++, any feedback is appreciated just don't be harsh please:(

So basically I am adding elements to a doubly linked list and in the function below my aim is to take its elements and add them into my singly linked list and return it, however I am just returning a pointer to the list.

I am just stuck on this little problem,

When I return it, it gives me an address, I am doing something wrong in the function, I just can put my finger on it, the rest of the code works with me fine. I hope I delivered my question correctly, ask me other wise. Any help is very appreciated.

    template <class Type>
    SinglyLinkedList<Type> *DoublyLinkedList<Type>:: Function()  {

    SinglyLinkedList<Type>* newList = new SinglyLinkedList<Type>();

    nodeDLL<Type>* p = head2;

    //I added the values from the doubly linked list into the singly linked list
    while (p !=NULL) {

    newList->addToHead(p->value2);

    p=p->next2;

    }

    // I used the print function to make sure the above works and it works the way I want,
    newList->print();

    // but when I return it here it gives me an address?
    return newList;

For reference here is the rest of the code:

class for the Singly linked list node


    template <class Type>
    class nodeSLL {

    public:

    nodeSLL* next;
    Type value;


    nodeSLL(Type v, nodeSLL* n) {

    value = v;
    next = n;

    }

    ~nodeSLL() {

    cout << "node destroyed" << endl;
    }


    };

Singly linked list class


    template <class Type>
    class SinglyLinkedList {

    public:

    nodeSLL<Type>* head;
    nodeSLL<Type>* tail;


    SingyLinkedList();
    SinglyLinkedList(SinglyLinkedList<Type> &obj);
    void addToHead(Type v);
    void addToTail(Type v);
    void print();

    ~SinglyLinkedList();
    };


template <class Type>
 SinglyLinkedList<Type>::SinglyLinkedList(const SinglyLinkedList &obj) // Copy Constructor
{
   head = obj.head;
   tail = obj.tail;

}



template <class Type>
SinglyLinkedList<Type>:: SinglyLinkedList() {

head = tail = NULL;

}
template <class Type>
SinglyLinkedList<Type>:: ~SinglyLinkedList() {

nodeSLL<Type> *p = head;
        while (p != NULL) {
            head = head->next;
            delete p;
            p = head;
        }

}

print function


    template <class Type>
    void SinglyLinkedList<Type>:: print() {
    nodeSLL<Type> * t = head;
    while (t != NULL) {
                cout << t->value << " => ";
                t = t->next;

            }
            cout << "  NULL"<< endl;


    }

add to head function


    template <class Type>
    void SinglyLinkedList<Type>:: addToHead(Type v) {

    nodeLL<Type> *newNode = new nodeSLL<Type>(v, head);


    if (head == NULL) {

    head = tail = newNode;

    } else {

    head = newNode;

    }


Doubly linked list node



    template <class Type>
    class nodeDLL {


    public:

    nodeDLL* next2;
    nodeDLL* prev2;
    Type value2;

    nodeDLL<Type>(nodeDLL<Type>* prv, Type v, nodeDLL<Type>* n) {

    value2 = v;
    next2 = n;
    prev2 = prv;

    }

    ~nodeDLL() {

    prev2 = next2 = NULL;
    cout << "node destroyed" << endl;
    }


    };

Doubly linked list class


    template <class Type>
    class DoublyLinkedList {

    public:

    nodeDLL<Type>*head2;
    nodeDLL<Type>*tail2;


    DounlyLinkedList();
    void addToHead2(Type v);
    void Clear();
    ~DoublyLinkedList();
    };

Add to head doubly linked list


    template <class Type>
    void DoublyLinkedList<T>:: addToHead2(Type v) {


    nodeDLL<T>* newNode = new nodeDLL<Type>(NULL, v, head2);
        if (head2 == NULL) { 
            head2 = tail2 = newNode;
        }
        else  { 
            head2->prev2 = newNode;
            head2 = newNode;
        }

    }


Main function


    int main()
    {

    SinglyLinkedList<int>* list1;
    DoublyLinkedList<int> lists;

    lists.addToHead2(55);
    lists.addToHead2(87);
    lists.addToHead2(2);


    //list1 = lists.Function();
    cout <<lists.Function();



        return 0;
    }


user5789
  • 3
  • 3

1 Answers1

0

It gives the address because that's what you wrote in the code. Don't use pointers if you don't want addresses. Do it like this instead

template <class Type>
SinglyLinkedList<Type> DoublyLinkedList<Type>::Function() {
    SinglyLinkedList<Type> newList;
    nodeDLL<Type>* p = head2;
    //I added the values from the doubly linked list into the singly linked list
    while (p !=NULL) {
        newList.addToHead(p->value2);
        p=p->next2;
    }
    // I used the print function to make sure the above works and it works the way I want,
    newList.print();
    return newList;
}

But to make this work your singly linked list class will need a valid copy constructor (because the list might be copied when you return from the function) and I see no evidence that you have that.

You need to read up on the rule of three before you go any further.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thanks for your answer! I read the link I realized I don't have a thorough understanding of copy constructors yet. I tried this but as we can't return a local object, how can a copy constructor help? I updated the code above and added the copy constructor, I am unsure if it's correct, and how to use it to return the list in the function? do I also need copy assignment operator? – user5789 Apr 04 '20 at 16:04
  • @user5789 You can return a local object. You can't return a reference or a pointer to a local object. When you return a local object it gets copied. Although the original gets destroyed a copy is made before that happens and the copy is what is returned. That's why the copy constructor is important. You don't need to do anything special to use the copy constructor, it gets used automatically. The code I posted above is correct, and doesn't need changing, provided you can write a good copy constructor. And yes you really should write a copy assignment operator as well. – john Apr 04 '20 at 16:15
  • Is this valid ? Because it's still not working :( template SinglyLinkedList::SinglyLinkedList(const SinglyLinkedList &obj) // Copy Constructor { head = obj.head; tail = obj.tail; } – user5789 Apr 04 '20 at 16:20
  • @user5789 No it's not. You have to copy the nodes of the list. Otherwise you end up with two lists sharing the same nodes, Then when one list is destructed it will delete the nodes and the other list will have a bunch of deleted nodes. – john Apr 04 '20 at 16:48