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

Spread the post

Insert a node after the Kth node from the end

Insert a given node after the kth node from the end in linked list.

For example Given linked list are contain following nodes.

root_ptr654321

Given 3 value of below function.

1) first node address of linked list,2) insert data value,3) and insert position

View pointers and nodes of tree.

Stack Areamaindata (int) ? position (int) ? root(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer)= NULL

After insert node 20 at position 2.

root_ptr62054321 Stack Areamaindata (int) =20position (int) =2root(pointer)nth_inserteddata (int) =20nth_last_node (int) =2temp(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer)= NULLstruct Nodedata (int)= 20next (pointer)

Try it Yourself

C program for insert node at a specific position from the end of linked list.


Output

view process

Stack Areamaindata (int) =10position (int) =0root(pointer)print_datatemp(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer)= NULLstruct Nodedata (int)= 20next (pointer) struct Nodedata (int)= 50next (pointer)

Try it Yourself

Spread the post

Recommended Posts: