Home > Back-end >  For the use of the new keyword cases in the data structure, C
For the use of the new keyword cases in the data structure, C

Time:10-07

Using new c + + to create a single table, in the loop body node, need not to need the delete, why data structure (YanWeiMin) materials, in creating a singly linked list and insert operation, with new generated nodes finally didn't delete, why?
Used to link node p or s amount is not in the middle? In function operation is complete (singly linked lists to establish good or insert complete) should not delete off after the link node p or s? Or for what reason?
Help bosses on

CodePudding user response:

Where was the wrote don't need to delete?
Now you posted example is linked list to create and insert, so there is no node to delete, you continue to see if there are any list delete example,
Delete the corresponding new
Corresponding malloc free
Your application you will have to release your memory,

CodePudding user response:

Is that you must delete unless you use a

STD: : from & lt; LNode> .

CodePudding user response:

Did you find the book, empty list operation.

Remove the node from the list, not necessarily to delete immediately, because will also use the data, use up, may also be reused, change numerically hang back? Only completely no need to delete it
That is true of empty list

Dare to doubt the author, don't blindly believe that is right

CodePudding user response:

Fun
reference 3 floor response:
you look for the book, is there a clear list of operating

Remove the node from the list, not necessarily to delete immediately, because will also use the data, use up, may also be reused, change numerically hang back? Only completely no need to delete it
That is true of empty list

Dare to doubt the author, don't blindly believe, is right

Thank you for your reply,
I don't know delete immediately, but I have a question is:
Create a list of the head pointer L don't have to immediately delete, of course, because the back of the main function of use, but in the process of creating a list pointer p or s (the pointer is in the process of create the list L) used to connect each node in the, when creating a list of function, complete list L after creation, the book did not use the delete to delete the auxiliary use pointer p or s, I want to ask this question, why? Because the pointer p space to be used on it (but not just use it to connect each new node of the data in the header pointer L open space behind?)

CodePudding user response:

Malloc or new is free and delete
Auxiliary pointer is not assigned, it just traverse the position of the positioning to operate the chain table in the middle of the variables, the intermediate variable because it have no new or malloc, natural don't need to delete or free

CodePudding user response:

reference 1st floor qybao response:
wrote don't need to delete the?
Now you posted example is linked list to create and insert, so there is no node to delete, you continue to see if there are any list delete example,
Delete the corresponding new
Corresponding malloc free
Your application you will have to release your memory,

Thank you for your reply,
This book didn't delete questions, but I read a book on example, not delete quantity among the pointer p or s
Just read the examples in this book singly linked lists are again, thought for a moment, and create a singly linked list of auxiliary pointer p not delete can be used for other function operation (the book example singly linked lists and use singly linked lists to create function, the order is a mess) with an auxiliary pointer, so there is no direct delete, and other functions directly in the haven't delete the pointer p, as a global pointer, just in a will use the function of application first, don't delete anyone can use,
But this book is not a complete program example, so finally didn't delete, write their own words in the main function delete off? Still don't have to delete, end of the program

CodePudding user response:

Fun
reference 5 floor response:
malloc or new is free and delete
Auxiliary pointer is not assigned, it just traverse the position of the positioning to operate the chain table in the middle of the variables, the intermediate variable because it have no new or malloc, natural don't need to delete or free

A ah, you look at the figure above, is to create a singly linked list (pointer to use the new p), one is to insert (also using the new pointer s)

CodePudding user response:

refer to the second floor 丁劲犇 response:
is that you must delete, unless you use a

STD: : from & lt; LNode> ,

Thank you for your reply,
This is what meaning, anyway, with the new will be sure to delete

CodePudding user response:

Create and insert, is a new allocation to a node, then put on the node list inside, it also use ah, so you can't delete the
Such as these nodes only pick from the list, no can delete

Do some operations, such as traversal
There will be some intermediate variables of this process, the intermediate variable pointing to the existing node, this also is not delete

CodePudding user response:

Fun, 9/f,
reference response:
create and insert, is a new allocation to a node, then hang this node list inside, it also use ah, so you can't delete the
Such as these nodes only pick from the list, no can delete

Do some operations, such as traversal
There will be some intermediate variables of this process, the intermediate variable pointing to the existing node, this also is not delete

Is so ah, I thought to create and insert the node of redistribution, hanging behind the head pointer L, after the data is in the L pointer address, so it among those who feel that the amount of pointer can delete, you explain so that's what I think is wrong (though I don't know why)

CodePudding user response:

CreateList_R this function inside the
One is L=new Node, this is the head of the linked list Node, to always use the
One is the inside of the loop repeatedly p=new Node; New here, r & gt; Next=p; to hang it on the list, behind will continue to use, can't delete,
P is temporary variables inside this is true, but the new node, the r - & gt; Next=p; Later, this will be linked list of nodes to management, and p have has nothing to do, so in the next cycle, p can also be again p=new Node

P is temporary variable that's true, but p corresponding to the new Node is not a temporary variable, but always use data

CodePudding user response:

You think about it, the intermediate variable exists in the list (or as a node in the list point to the same memory address), release the memory it is equivalent to the release of a node in the list of memory, very not easy to finish node application memory and connected to the chain in the table, the result is released, it also can achieve the result of inserting nodes?

CodePudding user response:

Your question is meaningless, if an object when it does not exist, it is only natural that the delete, if he still reason, so should not be delete

CodePudding user response:

Node is the memory block one by one, do you think of it as a train car good
New and delete is production and destroyed cars

Node, next is the hook below issues under a car
P is temporary, no temporary hook can throw, not what relation and carriages, and the Node Node under the next link is the key of

For with the create function, p no can
For (I=0; IR - & gt; Next=new Node;
Cin & gt;> R - & gt; Next - & gt; The data;
R - & gt; Next - & gt; Next=NULL;
R=r - & gt; Next;
};

P is the r - & gt; Next to write abandon long, temporary replacement, that's all

CodePudding user response:

thank you boss!

CodePudding user response:

On the 14th floor fun
reference response:
node is one memory block, do you think of it as a train car good
New and delete is production and destroyed cars

Node, next is the hook below issues under a car
P is temporary, no temporary hook can throw, not what relation and carriages, and the Node Node under the next link is the key of

For with the create function, p no can
For (I=0; IR - & gt; Next=new Node;
Cin & gt;> R - & gt; Next - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull