Linked List in C- Delete Node at Head in Linked List

Linked List in C- Delete Node at Head in Linked List

Linked List in C- Delete Node at Head in Linked List

DATA STRUCTURES IN C – LINKED LIST (20 PROGRAMS)

Linked List in C- Delete Node at Head in Linked List :

Source Code :

#include<stdio.h>
struct node *head=NULL;
//Declared as global variable since it is accessed by multiple functions

struct node
{
    //Structure named node to store integer in variable data and a pointer variable which can store address location of next node
    	int data;
	    struct node *next;
	    //Pointer variable next is a self referential pointer which stores the address of structure node
};

void delete_at_head()
{
    	//Function which deletes first node in the linked list
    	struct node* temp=head;
	    //Use a new pointer variable temp
    	//Do not use the pointer variable head directly. If we use it, the address location of the first node in the linked list will be lost

    	struct node* holder;
    	//Pointer to store the next node of the node which we are going to delete

    	if(temp!=NULL)
    {
        		//Executed only if the linked list has atleast one node
        		holder=temp->next;
        		//Assigning the next parameter of the node which we are going to delete

        		free(temp);
        		//free method is used to release the memory location
        		//Since we are deleting a node, we need to manually free the memory of the node which we are going to delete

        		head=holder;
        		//Making the head pointer point to next node of deleted node
    	}
	    else
    	{
        		printf("\nLinked list is empty");
    	}
}

void print()
{
    	//This funtion is used to print the data in each of the node in the linked list
    	//Process of moving from one node to another node is called as traversing
	struct node *temp=head;
    	printf("\nList:");
    	while(temp!=NULL)
    	{
        		printf("\n%d ",temp->data);
        		temp=temp->next;		
    	} 
}

int main()
{

    	struct node *one = (struct node*)malloc(sizeof(struct node));
    	struct node *two = (struct node*)malloc(sizeof(struct node));
    	struct node *three = (struct node*)malloc(sizeof(struct node));
    	//Creating 3 nodes one, two, three
    	//Dynamic memory allocation
    	//Memory for these three nodes are allocated in heap.Not stack

    	head=one;
    	//Making the head pointer point to first node

    	one->data=1;
    	one->next=two;
    	//node one has data 1
    	//node one points to node two

    	two->data=2;	
    	two->next=three;
    	//node two has data 2
    	//node two points to node three

    	three->data=3;
    	three->next=NULL;
    	//node three has data 3
    	//node three points to NULL i.e. end of linked list

    	//one->two->three

    	print();

    	delete_at_head();
    	//Delete first node in the linked list
    	print();

    	delete_at_head();
    	//Again delete first node in the linked list
    	print();

    	delete_at_head();
    	//Again delete first node in the linked list
    	print();

    	delete_at_head();
    	//Again delete first node in the linked list
    	//But now the linked list is empty

    	return 0;
}

Output :

List:
1
2
3

List:
2
3

List:
3

List:

Linked list is empty

[ YOU MAY ALSO LIKE ]

Leave a Reply