Spread the post

Program to delete first node in linked list

Delete first node of given linked list, suppose linked list contain following nodes [1,2,3,4,5,6,7,8].

root_ptr87654321

After delete first node linked list is.

root_ptr8765432

After delete first node.

root_ptr876543

View insertion and deletion of first node process

Try it yourself

Process to delete

1) root are point to first node of linked list. used one another variable remove_ptr pointer there are also point to first node of linked list.

struct Node*remove_ptr=root;

2) root are move to next node because delete first node.

root=remove_ptr->next; //remove_ptr are point to first node

3) Before delete first node. change next pointer to NULL.

remove_ptr->next=NULL;

4) free first node.

free(remove_ptr);

5) After delete remove_ptr is NULL.

remove_ptr=NULL;

Function to delete first node.

Time complexity of this program O(1)

C Program to delete first node in linked list.


OutPut


Stack Areamainroot(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) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer)= NULL

After delete First node.

Stack Areamainroot(pointer)delete_firstremove_ptr(pointer) NULLroot(pointer) Heap Areastruct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer)= NULL

After delete First node.

Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer)= NULL

Try it yourself

Spread the post

Recommended Posts: