+ 3

Can we delete a node in linked list without using free method?

I want to perform deletion of node from a linked list where the node should be deleted manually without using free() method

11th Nov 2018, 11:20 AM
Aksfh
7 Respostas
+ 7
Like others have sad. This is not good idea. But have a way to do it. But not in a implementation with heap memory (malloc). The one way to delete without free method is not using manual allocation (malloc). You will need to implement using memory in stack( arr[]). But you can't hold many numbers this way because stack memory is limited (plataform depedent). After that you should do a pseudo deletition (Using flags for sinalize deleted members)
11th Nov 2018, 12:03 PM
Anya
Anya - avatar
+ 6
You could add the node to an available list you maintain of nodes to be reused and, instead of allocating a new one the next time one is needed, reuse one on your list. Only allocating a new one when the available list is empty.
11th Nov 2018, 4:58 PM
John Wells
John Wells - avatar
+ 5
It depends how your linked list is implemented. If a new node is allocated, then you should not as it would be a memory leak. If your list is in a contiguous memory space, then you only free it when the list is deleted
11th Nov 2018, 11:43 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 5
In languages such as Java and C# which utilizes garbage collector to free up unused memory, it gets done automatically. But C/C++ doesn't have such built-in luxury to do so. Also, the memory that gets allocated by, for example, "malloc(sizeof(struct node))" hasn't the exact size of the node, but it contains other housekeeping information (e.g. pointer to next and previous heap block, guard bytes, etc.), too. Therefore, there's no way to interfere with/manual control the process of freeing up the memory without using free().
11th Nov 2018, 11:49 AM
Babak
Babak - avatar
+ 4
Let's look at what you're intending to do and why? How do you benefit? A few clock cycles saved? It's easier to "join" C to A after freeing B. And for quick 'n dirty, you could overwrite data in B and keep things as-is. Anyway, I get a feeling you're not too familiar, so here's an example I did some time back to help understand the concepts for basic stuff (actually was intended as a struct-related example but grew): https://code.sololearn.com/c0aLC4piZ8L4/?ref=app Once you understand it better, you see the futility in what you want to do. You should also know, as said, by not de-allocating memory, you risk overflows, leaks, security exploits, etcetera.
12th Nov 2018, 12:44 AM
non
+ 1
Yes U can you need to make a method to delete the node. But it is not a good idea when you have a free method why waste your time
11th Nov 2018, 4:20 PM
Mohammad Wasiq
Mohammad Wasiq - avatar
+ 1
As John Wells said, you could reuse your old nodes by keeping references to them in an array or something (although you may need to run that array like a queue or a pile, which adds the complexity of another data structure). Obviously if your program is not meant to run for more than a second and only handles a few hundred elements (as in, this is something you're cobbling together for a very specific scenario where freeing the memory isn't wanted) then you could simply NOT free the node after removing it from the chain. As long as you know that it is very very bad practice and that the system will only get back that memory after your program exists, then you can do it. The chain will still function, it will just be more and more costly in memory the longer it's being used. Cheers!
11th Nov 2018, 11:04 PM
Salah Eddine H
Salah Eddine H - avatar