Click here to Skip to main content
15,898,371 members

Comments by xumepoc (Top 17 by date)

xumepoc 15-Aug-15 14:37pm View    
>Your original question was about avoiding the list class, which is the second class, right?
>Why do you want that? Basically what you are expecting here is what we call unexpected. You may take the data out of a node and keep that in some other class, but keeping a node is an entirely different thing. A node contains data and pointers to its next and previous nodes in the linked list. How do you plan to ensure that the Next and Previous of such a node are valid pointers? Who will be responsible for deleting this node? How will you ensure that this node is not stored in some other objects too?

Well regarding the list class, what I meant was that this class will do the insertion, but will not store the nodes and will not be the one responsible for the memory management. The linked list will be created once based on a specific event and on the second event, will remove the entire list and create it again. But after reading you second question I see that this will be a problem, because there is no way as you said that the third party class using one node will have a valid node or one that was deleted on the rise of the event.

>Basically Node is a class and previous node, current node and next node are different objects of the same class. When you allocate memory for *prev and *next from node, you are actually asking one 'object' of a class to create other 'objects' of the same class which spoils the basic purpose of classes and objects.
More over how do you plan to implement this functionality in a Node? And how do plan to manage these objects? Who will keep track of the objects and how? Who will be responsible for deleting these objects? What happens to the next and previous of a node when the node dies? When a node, say node1 creates its previous (say node0) and next (node2) nodes, how do you plan to manage the previous and next part of node0 and node2 (the next of node0 and prev of node2 should be node1)?

This basically was my question if this was possible and if yes how :) :) :)

I really appreciate you taking from your time answering my questions, I guess I really need to use linkedlist class and node class and work with that....:)
xumepoc 14-Aug-15 1:26am View    
Is it mandatory to have a reference to the head and tail node? Shouldn't they be just null? Surly I can find them easy with a simple while loop until reaching the null element?

A second class will take care of the insertions to the list. But I need a Node to live beyond the life of that second class. So let say I have the LinkedList class creating a list of 5 nodes. I need to pass the first node to a another class and store it there. But then if the LinkedList is destroyed and it was his responsibility for the memory management it will delete also that first node, resulting in a unexpected behavior.

Can you explain if possible, why is it wrong and not advisable for the node to create it's prev and next elements? And for the creation of the first Node responsible will be the same class from above.
xumepoc 13-Aug-15 8:39am View    
I have read that example, but it is not helping me with the problem I have. I need the Node class to be "standalone". I need example with the allocation of the "next" node performed in the Node class itself. I know the idea and the basic implementation of the linked list, but I can't wrap my mind around the memory allocation when you add new node, when you need to free the memory in the destructor, the memory and node management in "operator=" and copy constructor.
xumepoc 13-Aug-15 8:21am View    
I did, but all I could find were examples with LinkedList class...
xumepoc 10-Aug-15 6:48am View    
And for the deep copy function and the operator= I just need to loop over and use the add to back routine? Also the deconstruct function should do the same, but just delete the next correct?