-
Hello, I hope everyone's here is doing good. I'm not much of a forum guy but I have a question to ask and I can't seem to figure an answer on my own.
The thing is, for certain assignment I need to use a doubly linked list, I have done it already in NetBeans 8.2 (C++) and works just fine but for some reason the assignation of value of the right pointer from the 2nd node until the end doesn't seem to work in QT. It compiles and runs but doesn't do the link from one node to another one at all.
It links the first one with the second one and backwards, and it links the third one with the second one but not the second one with the third and so on.Here is a copy of the code that I doesn't seem to work on QT. (Yes, is a template)
template<class T> class ListaAux; template<class T> class Node{ private: Node<T> * LeftLink; Node<T> * RightLink; T info; public: Node(); friend class ListaAux<T>; }; template <class T> NodoDoble<T>::NodoDoble(){ LigaDer = NULL; LigaIzq = NULL; } template <class T> class ListaAux{ private: Node<T> * first; Node<T> * last; public: ListaAux(); void Insert(T data); //More functions go here but aren't relevant for this matter template <class T> void ListaAux<T>::Insert(T data){ if(first==NULL){ //Insert first in case is the first element to ever come Node<T> * nuevo = new Node<T>(); nuevo->info = data; first= nuevo; last= nuevo; } else{ //Insert code if it is the second item to be inserted if(first==last){ Node<T> * nuevo = new Node<T>(); nuevo->info=dato; nuevo->LeftLink=first; first->RightLink=nuevo; last=nuevo; } else{ //This inserts the new node at the very end of the list //Supposed to insert anything else that may come Node<T> * nuevo = new Node<T>(); nuevo->info=data; nuevo->LeftLink=last; last->RightLink=nuevo; last=nuevo; //This three last lines don't seem to link... //...the past last node right link with the location of the new one. } } }
Ignore any typos, or names that doesn't match, I could have missed something translating the names for more understandability but I assure you that my source code compiles and works completely fine except for the issue I mentioned.
Maybe I am missing something in QT that is different with pointers, I don't know but please let me know. Thanks for reading. : )
[Moved to C++ Gurus ~kshegunov]
-
Hello, I hope everyone's here is doing good. I'm not much of a forum guy but I have a question to ask and I can't seem to figure an answer on my own.
The thing is, for certain assignment I need to use a doubly linked list, I have done it already in NetBeans 8.2 (C++) and works just fine but for some reason the assignation of value of the right pointer from the 2nd node until the end doesn't seem to work in QT. It compiles and runs but doesn't do the link from one node to another one at all.
It links the first one with the second one and backwards, and it links the third one with the second one but not the second one with the third and so on.Here is a copy of the code that I doesn't seem to work on QT. (Yes, is a template)
template<class T> class ListaAux; template<class T> class Node{ private: Node<T> * LeftLink; Node<T> * RightLink; T info; public: Node(); friend class ListaAux<T>; }; template <class T> NodoDoble<T>::NodoDoble(){ LigaDer = NULL; LigaIzq = NULL; } template <class T> class ListaAux{ private: Node<T> * first; Node<T> * last; public: ListaAux(); void Insert(T data); //More functions go here but aren't relevant for this matter template <class T> void ListaAux<T>::Insert(T data){ if(first==NULL){ //Insert first in case is the first element to ever come Node<T> * nuevo = new Node<T>(); nuevo->info = data; first= nuevo; last= nuevo; } else{ //Insert code if it is the second item to be inserted if(first==last){ Node<T> * nuevo = new Node<T>(); nuevo->info=dato; nuevo->LeftLink=first; first->RightLink=nuevo; last=nuevo; } else{ //This inserts the new node at the very end of the list //Supposed to insert anything else that may come Node<T> * nuevo = new Node<T>(); nuevo->info=data; nuevo->LeftLink=last; last->RightLink=nuevo; last=nuevo; //This three last lines don't seem to link... //...the past last node right link with the location of the new one. } } }
Ignore any typos, or names that doesn't match, I could have missed something translating the names for more understandability but I assure you that my source code compiles and works completely fine except for the issue I mentioned.
Maybe I am missing something in QT that is different with pointers, I don't know but please let me know. Thanks for reading. : )
[Moved to C++ Gurus ~kshegunov]
There's not a line of Qt specific code here. That's all plain C++.
Have you traced it in the debugger?Beside the obvious simplification:
template <class T> void ListaAux<T>::Insert(T data) { auto createNode = [this] (const T & data) -> Node<T> * { Node<T> * node = new Node<T>(); if (last) last->RightLink = node; node->LeftLink = last; node->info = data; return node; }; last = createNode(data); if (!first) first = last; }
I didn't spot anything obviously wrong, but then again I'm no debugger.
-
There's not a line of Qt specific code here. That's all plain C++.
Have you traced it in the debugger?Beside the obvious simplification:
template <class T> void ListaAux<T>::Insert(T data) { auto createNode = [this] (const T & data) -> Node<T> * { Node<T> * node = new Node<T>(); if (last) last->RightLink = node; node->LeftLink = last; node->info = data; return node; }; last = createNode(data); if (!first) first = last; }
I didn't spot anything obviously wrong, but then again I'm no debugger.
@kshegunov Thanks for taking time to move my question and replying. I did try to use the debugger and is completely ignoring the line that sets the last node right link to aim at the new one. I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.
-
There's not a line of Qt specific code here. That's all plain C++.
Have you traced it in the debugger?Beside the obvious simplification:
template <class T> void ListaAux<T>::Insert(T data) { auto createNode = [this] (const T & data) -> Node<T> * { Node<T> * node = new Node<T>(); if (last) last->RightLink = node; node->LeftLink = last; node->info = data; return node; }; last = createNode(data); if (!first) first = last; }
I didn't spot anything obviously wrong, but then again I'm no debugger.
@kshegunov said in Issue with pointers in doubly linked lists.:
There's not a line of Qt specific code here. That's all plain C++.
Have you traced it in the debugger?Beside the obvious simplification:
template <class T> void ListaAux<T>::Insert(T data) { auto createNode = [this] (const T & data) -> Node<T> * {
Out of curiosity, what's the motivation for using a lambda to initialize the new node rather than running the code directly in Insert()?
Given that the lambda is destroyed when the function returns, I wonder if any compiler will inline it and optimize out the capture.
follow up: godbolt.org says yes, at least for some versions of gcc and clang.
-
@kshegunov Thanks for taking time to move my question and replying. I did try to use the debugger and is completely ignoring the line that sets the last node right link to aim at the new one. I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.
@Lonewolf46 said in Issue with pointers in doubly linked lists.:
I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.
Did you forget to initialize
first
andlast
tonullptr
?@jeremy_k said in Issue with pointers in doubly linked lists.:
Out of curiosity, what's the motivation for using a lambda to initialize the new node rather than running the code directly in Insert()?
No reason in particular. Started writing something, stopped, started again, so it just stuck trough.
@jeremy_k said in Issue with pointers in doubly linked lists.:
Given that the lambda is destroyed when the function returns, I wonder if any compiler will inline it and optimize out the capture.
Yes, every decent optimizier is going to do it. (
-O2
implied) -
@Lonewolf46 said in Issue with pointers in doubly linked lists.:
I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.
Did you forget to initialize
first
andlast
tonullptr
?@jeremy_k said in Issue with pointers in doubly linked lists.:
Out of curiosity, what's the motivation for using a lambda to initialize the new node rather than running the code directly in Insert()?
No reason in particular. Started writing something, stopped, started again, so it just stuck trough.
@jeremy_k said in Issue with pointers in doubly linked lists.:
Given that the lambda is destroyed when the function returns, I wonder if any compiler will inline it and optimize out the capture.
Yes, every decent optimizier is going to do it. (
-O2
implied)@kshegunov said
Did you forget to initialize
first
andlast
tonullptr
?Negative, those where already perfectly initialized. All it took was to change the input data and it worked; which if you are wondering was a char but the method I was using to retrieve it from the data obtained was the reason for it to break. Really uncanny but I am happy that it works now and thankful to you for replying to begin with.