c++ - Using the destructor to free linked objects -


I have a class named "node". I link a group of node objects to create a link list. When called "node" catalyst, it only removes the first node. How can I be repeated from the entire linked list of nodes and remove each node object?

Here is the definition of class:

  class node {private: double coffe; Intel exponent; Node * next; Public: node (double c, int e, node * nodobject) {coeff = c; Exponent = e; Next = nodeobjectPtr; } ~ Node () {printf ("node destroyed"); }   

The destructor is called to remove the pointer on the node before the linked node list.

Since you do not know how many nodes are in the list, if you do not have firm obligation, then It is not a good idea to make methods continuously recursive, because each call uses some stack space, and when the available stack space ends, you get undefined behavior like an accident.

So if you want to diagnose the nodes in the node's destructor, then first you need to unlink each node first, delete it.

It can be:

  node * unlink (node ​​* and p) {node * result = p; P = p-> next; Results-> gt; Next = nalper; Return result; } Node :: ~ node () {while (next! = Nullptr) {delete unlink (next); }}   

But better, create a list object owned by nodes in a written list.

Of course, just use a std :: vector as long as it is for learning purposes or to have a very good reason to roll your own linked list (And yes, I mean std :: list ).

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -