Linked List in C- Delete Last Node in Linked List

Linked List in C- Delete Last Node in Linked List

Linked List in C- Delete Last Node in Linked List

DATA STRUCTURES IN C – LINKED LIST (20 PROGRAMS)

Linked List in C- Delete Last Node 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_last_node()
{
    	//Function to delete last node in the linked list
    	struct node *temp=head;
    	//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
    	//Pointer variable temp is used to traverse the linked list

    	struct node *holder=head;
    	//Pointer to node which stores the address location of previous node of temp

    	if(temp!=NULL)
    	{	
        		temp=temp->next;
        		if(temp==NULL)
        		{
            			//The condition becomes true when the linked list has only one node
            			head=NULL;
            			//If the linked list has only one node, deletion of that single node should make the head pointer point to NULL

            			free(holder);
            			//free method is used to release the moemory
            			//The memory occupied by last single node is released
            			return;
           			//return since we dont have anything to do further in the function
			           //The program works even without the statement return			
       		}

       		while(temp!=NULL)
       			{
           				if(temp->next!=NULL)
           				{
               					temp=temp->next;
               					holder=holder->next;
           				}		
           				else
           				{
               					break;
           				}		
       			}
       		holder->next=NULL;
       		//Making the last but previous node point to NULL

       		free(temp);	
       //free method is used to release the moemory
       		//The memory occupied by last node is released
   	 }
    	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_last_node();
    	print();
    	//Delete last node in the linked list

    	delete_last_node();
    	print();
    	//Delete last node in the linked list

    	delete_last_node();
    	print();
    	//Delete last node in the linked list

	    delete_last_node();
    	print();
    	//Delete last node in the linked list

    	return 0;
}

Output :

List:
1
2
3

List:
1
2

List:
1

List:

Linked list is empty
List:

[ YOU MAY ALSO LIKE ]

Leave a Reply