Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

delete all nodes in linked list

Before end of program execution delete all linked list node. because the best way to utilize heap memory are allocation and deallocation.

Advantages

1) Overcome of memory leak problem.

2) Efficiently use dynamic memory allocation on heap.

Most of tool are detect memory leak of your program. Valgrind is one of the popular tool to detect memory leak.

let us discuss about how to delete linked list node.

Given a Linked list.

root_ptr4020103080706050

In this linked list 8 nodes. view insertion process.

Try it yourself

Algorithm to delete node:

Explain:

Use one temp pointer.

step 1: Assign the temp pointer value is root node value.

step 2: move root to next memory block and free temp pointer.

continue this steps when root is not NULL.

Use free() function to delete linked linked list node. this function takes address of delete node.

c program to delete all nodes of given linked list.

Output

Code execution: view code execution process.

Stack Areamainroot(pointer)remove_noderoot(pointer)temp(pointer) Heap Areastruct Nodedata (int)= 50next (pointer) struct Nodedata (int)= 60next (pointer) struct Nodedata (int)= 70next (pointer) struct Nodedata (int)= 80next (pointer) struct Nodedata (int)= 30next (pointer) struct Nodedata (int)= 10next (pointer) struct Nodedata (int)= 20next (pointer) struct Nodedata (int)= 40next (pointer)= NULL

Note that not given all step of execution process here. view more.

Try it yourself

Spread the post

Recommended Posts: