0

How to return a pointer ?

Now I am unsure about how to do below as Iam fairly new to C++ I’m trying to do a function that returns a linked list, however in the method, I am asked to do, the return type includes a pointer // copying the values from the doubly linked list to the singly linked list then returns the singly linked list. However, it’s not returning anything, what am I doing wrong? SinglyLinkedList<T>* DoublyLinkedList<T>:: Function() { SinglyLinkedList<T> list1; DLLnode<T>*p = head; while ( p!= NULL ) { list1.addtoHead(p->value); p=p->next; } Return list1; } For example Original doubly linked list 1 <=> 2 <=> 3 The returned singly linked list 1 -> 2 -> 3 error message given: *cannot convert SinglyLinkedList<int> to SinglyLinkedList<int>* in return

6th Apr 2020, 11:02 AM
Noura
Noura - avatar
7 Answers
0
Can you post your code please? Because I don't think you would need to return it as pointer.
6th Apr 2020, 11:23 AM
Mustafa K.
Mustafa K. - avatar
0
Mustafa K. thanks for your comment! sure I will edit the post but that might take a little because I am using my phone, however I was given the function declaration in class and told to return the singly linked list but I don’t understand it quite well or how to make this idea work.
6th Apr 2020, 11:27 AM
Noura
Noura - avatar
0
Noura Can you try creating your list1 as pointer because your function wants it pointer, or you simply delete asterisk sign here SinglyLinkedList<T>* DoublyLinked<T>::Function()
6th Apr 2020, 11:32 AM
Mustafa K.
Mustafa K. - avatar
0
I tried this now, I had to do some changes to it for example list1->addtoHead but when I compile it gives me segmentation fault , however when I tried yesterday it gave me an address instead of the list itself
6th Apr 2020, 11:37 AM
Noura
Noura - avatar
6th Apr 2020, 11:37 AM
Noura
Noura - avatar
0
Noura If it gives you segmentation fault then that means you are trying to use a place at ram before you reserve it. You should do something like that in your list in addtohead: You need to create new node, node* temp = new node( data ); Then you need to, head->next = temp; Do something like this then it does work. edit: then of course you need to change the place of tail too
6th Apr 2020, 11:41 AM
Mustafa K.
Mustafa K. - avatar
0
the underlying problem here is that compiler always make a stack for a function call. as soon as the function exits, the function stack get removed which causes all the local variables of functions goes out of scope. ( which means that the local variable "list1" no longer exist ). so we can fix that by either : using the new operator ( this is generally a bad idea. If you must do it this way then maybe you can use a smart pointer ): SinglyLinkedList<T>* DoublyLinkedList<T>:: Function() { SinglyLinkedList<T>* list1 = new SinglyLinkedList<T>(); DLLnode<T>*p = head; while ( p!= NULL ) { list1->addtoHead(p->value); p=p->next; } Return list1; } or by making it static. (as static variables have a property of preserving their value even after they are out of their scope): SinglyLinkedList<T>* DoublyLinkedList<T>:: Function() { static SinglyLinkedList<T> list1; DLLnode<T>*p = head; while ( p!= NULL ) { list1.addtoHead(p->value); p=p->next; } Return &list1; }
6th Apr 2020, 12:36 PM
MO ELomari
MO ELomari - avatar