Example : A data structure program using C to create a single inked list having five nodes to store five records and display them using while loops.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *home, *link, *newnode;
void main()
{
   int i;  
	
   //single Node creation code
   home= (struct student*)malloc(sizeof(struct student));
   printf("Enter Roll No. and Student name = ");
   scanf("%d%s",&home->srollno,home->sname);
   home->ptr=NULL;		
	
   link=home;
   for(i=1;i<5;i=i+1)
   {
	newnode= (struct student*)malloc(sizeof(struct student));
	printf("Enter Roll No. and Student name = ");
	scanf("%d%s",&newnode->srollno,newnode->sname);
	newnode->ptr=NULL;
		
	link->ptr=newnode;
	link=link->ptr;			
   }
	
   //Display code
   printf("\nThe Outputs are = \n");
   link=home;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }	
   getch();
}

Output :
Enter Roll No. and Student name = 100
A
Enter Roll No. and Student name = 200
B
Enter Roll No. and Student name = 300
C
Enter Roll No. and Student name = 400
D
Enter Roll No. and Student name = 500
E

The Outputs are =
100  A
200  B
300  C
400  D
500  E
Example : A data structure program using C to create a single linked list having nodes equal to user’s given value and display them.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *home, *link, *newnode;
void main()
{
   int i,num;	
   printf("Enter the value for nodes in the Linked List = ");
   scanf("%d",&num);
	
   //single Node creation code
   home= (struct student*)malloc(sizeof(struct student));
   printf("Enter Roll No. and Student name = ");
   scanf("%d%s",&home->srollno,home->sname);
   home->ptr=NULL;		
	
   link=home;
   for(i=1;i<num;i=i+1)
   {
	newnode= (struct student*)malloc(sizeof(struct student));
	printf("Enter Roll No. and Student name = ");
	scanf("%d%s",&newnode->srollno,newnode->sname);
	newnode->ptr=NULL;
		
	link->ptr=newnode;
	link=link->ptr;			
   }
	
   //Display code using while loop
   printf("\nThe Outputs are = \n");
   link=home;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }
	
   //OR ----  Display code using for loop  ----
   printf("\nThe Outputs are = \n");
   for(link=home;link!=NULL;link=link->ptr)   
   {
	printf("%d  %s\n",link->srollno,link->sname);			
   }	
   getch();
}

Output :

Enter the value for nodes in Linked List = 3
Enter Roll No. and Student name = 101
A
Enter Roll No. and Student name = 201
B
Enter Roll No. and Student name = 301
C

The Outputs are =
101  A
201  B
301  C
Example : A data structure program using C to create a single linked list having nodes equal to user’s given value and display them using function.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link;
	
void create(struct number *);
void display(struct number *);

int i,x,count=0;
   
void main()
{
   printf("Enter the Node value for a Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;
   struct number *newnode;
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{
   printf("\nThe Outputs are = \n");
   for(link=home2;link!=NULL;link=link->ptr)   
    {
       printf("%d ",link->num);	
    }      
}
Output :
Enter the Node value for a Linked List = 3
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30

The Outputs are =
10 20 30
Example : A data structure program using C to count the no. of nodes/find the length of the existing linked list.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *home, *link, *newnode;
void main()
{
	int i,num,count=0;
	
	printf("Enter the value for nodes in Linked List = ");
	scanf("%d",&num);
	
	home= (struct student*)malloc(sizeof(struct student));
	printf("Enter Roll No. and Student name = ");
	scanf("%d%s",&home->srollno,home->sname);
	home->ptr=NULL;		
	
	link=home;
	for(i=1;i<num;i=i+1)
	{
		newnode= (struct student*)malloc(sizeof(struct student));
		printf("Enter Roll No. and Student name = ");
		scanf("%d%s",&newnode->srollno,newnode->sname);
		newnode->ptr=NULL;
		
		link->ptr=newnode;
		link=link->ptr;			
	}		
		link=home;
	while(link!=NULL)
	{
		count=count+1;
		link=link->ptr;		
	}
        printf("\nThe Total Nodes in this Linked List are = ");
	printf("%d",count);
	
	getch();
}
Example : A data structure program using C to display the first and last node values of the existing singly linked list.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct student
{
  int srollno;
  char sname[50];
  
  struct student *ptr;  
};
   
struct student *home, *link, *newnode;
void main()
{
	int i,num;
	
	printf("Enter the value for nodes in Linked List = ");
	scanf("%d",&num);
	
	home= (struct student*)malloc(sizeof(struct student));
	printf("Enter Roll No. and Student name = ");
	scanf("%d%s",&home->srollno,home->sname);
	home->ptr=NULL;		
	
	link=home;
	for(i=1;i<num;i=i+1)
	{
		newnode= (struct student*)malloc(sizeof(struct student));
		printf("Enter Roll No. and Student name = ");
		scanf("%d%s",&newnode->srollno,newnode->sname);
		newnode->ptr=NULL;
		
		link->ptr=newnode;
		link=link->ptr;			
	}
	printf("\nThe First & Last Node Values of this Linked List are = \n");
	link=home;		
	printf("%d %s\n",link->srollno,link->sname);

	while(link->ptr!=NULL)
	{		
	   link=link->ptr;		
	}
	printf("%d %s\n",link->srollno,link->sname);	
	getch();
}
Example : A data structure program using C to search an element from the existing singly linked list using function.
#include <stdio.h>  
#include <conio.h>
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link, *newnode;
	
void create(struct number *);
void search(struct number *);

int i,x,y;
   
void main()
{
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);   
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
         
   search(home);
      
   getch();
}

void create(struct number *home1)
{
   link=home1;   	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void search(struct number *home2)
{   
   int count=0;
    printf("\nEnter the searched value = ");
    scanf("%d",&y);
	  
   for(link=home2;link!=NULL;link=link->ptr)   
    {        
        if(link->num==y)
	   count++;			
    } 
    
	if(count==0)
	   printf("\nValue Not found");
	else
	   printf("\nValue found");
	  
}


Output :
Enter the value for nodes in Linked List = 3
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30

Enter the searched value = 30

Value found
Example : A data structure program using C to display only odd node value from the existing singly linked list.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link, *newnode;
void main()
{
   int i,x,count=0;	
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);
	
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;		
	
   link=home;
   for(i=1;i<x;i++)
   {
	newnode= (struct number*)malloc(sizeof(struct number));
	printf("Enter Integer Numeric Value = ");
	scanf("%d",&newnode->num);
        newnode->ptr=NULL;
		
	link->ptr=newnode;
	link=link->ptr;			
   }
	
   printf("\nThe Outputs are = \n");
   for(link=home;link!=NULL;link=link->ptr)   
   {
	 count=count+1;
	 if(count%2!=0)
	    {
	      printf("%d ",link->num);	
	    }		
   }   
   getch();
}

Output :
Enter the value for nodes in Linked List = 5
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
Enter Integer Numeric Value = 50

The Outputs are =
10 30 50
Example : A data structure program using C to display only odd node value from the existing singly linked list using function/user defined function (UDF).
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link;
	
void create(struct number *);
void display(struct number *);

int i,x,count=0;
   
void main()
{
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   display(home);
   
   getch();
}

void create(struct number *home)
{
   link=home;
   struct number *newnode;
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home)
{
   printf("\nThe Outputs are = \n");
   for(link=home;link!=NULL;link=link->ptr)   
    {
       count=count+1;
       if(count%2!=0)
        {
            printf("%d ",link->num);	
	}		
    }   
}
Example : A data structure program using C to insert a new node in the beginning of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link, *newnode;
	
void create(struct number *);
void display(struct number *);
void insertatbeg(struct number *);

int i,x;
   
void main()
{
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   printf("The output before insertion are = ");
   display(home);
   
   insertatbeg(home);
   printf("The output after insertion are = ");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;   
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{   
   for(link=home2;link!=NULL;link=link->ptr)   
    {        
        printf("%d ",link->num);			
    }   
}

void insertatbeg(struct number *home3)
{
   
    newnode= (struct number*)malloc(sizeof(struct number));
    printf("\nEnter Integer Numeric Value for new node to insert at beginnign = ");       
    scanf("%d",&newnode->num);
    newnode->ptr=NULL; 
	
    newnode->ptr=home3;
    home=newnode; 
}
Output :
Enter the value for nodes in Linked List = 3
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
The output before insertion are = 10 20 30
Enter Integer Numeric Value for new node to insert at beginnign = 05
The output after insertion are = 5 10 20 30
Example : A data structure program using C to insert a new node in the middle of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link, *newnode;
	
void create(struct number *);
void display(struct number *);
void counting(struct number *);
void insertatmid(struct number *);

int i,x,count=0,mid;
   
void main()
{
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   printf("The output before insertion are = ");
   display(home);
   
   counting(home);
   
   insertatmid(home);
   printf("The output after insertion are = ");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;   
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{   
   for(link=home2;link!=NULL;link=link->ptr)   
    {        
        printf("%d ",link->num);			
    }   
}

void counting(struct number *home3)
{   
   for(link=home3;link!=NULL;link=link->ptr)   
    {        
        count=count+1;		
    } 
	mid=count/2;	  
}

void insertatmid(struct number *home4)
{
   	link=home4;   	
   	newnode= (struct number*)malloc(sizeof(struct number));
	printf("\nEnter Integer Numeric Value for new node to insert at middle = ");       
        scanf("%d",&newnode->num);
        newnode->ptr=NULL; 
	
	for(i=1;i<mid;i++)
	{
		link=link->ptr;
	}
	newnode->ptr=link->ptr;	
	link->ptr=newnode; 
}
Output :
Enter the value for nodes in Linked List = 5
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
Enter Integer Numeric Value = 50
The output before insertion are = 10 20 30 40 50

Enter Integer Numeric Value for new node to insert at middle = 100
The output after insertion are = 10 20 100 30 40 50
Example : A data structure program using C to insert a new node in the last/end of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link, *newnode;
	
void create(struct number *);
void display(struct number *);
void insertatlast(struct number *);

int i,x;
   
void main()
{
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   printf("The output before insertion are = ");
   display(home);  
   
   insertatlast(home);
   printf("The output after insertion are = ");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;   
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{   
   for(link=home2;link!=NULL;link=link->ptr)   
    {        
        printf("%d ",link->num);			
    }   
}

void insertatlast(struct number *home4)
{   	  	
   	struct number *home5;
	newnode= (struct number*)malloc(sizeof(struct number));
	printf("\nEnter Integer Numeric Value for new node to insert at end = ");       
        scanf("%d",&newnode->num);
        newnode->ptr=NULL; 
	
	for(link=home4;link->ptr!=NULL;link=link->ptr)  
	{		
	}	
	link->ptr=newnode;	 
}
Output :
Enter the value for nodes in Linked List = 3
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
The output before insertion are = 10 20 30
Enter Integer Numeric Value for new node to insert at last= 40
The output after insertion are = 10 20 30 40
Example : A data structure program using C to insert a new node at customized/user’s choice location of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h> 
#include <stdlib.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link, *newnode;
	
void create(struct number *);
void display(struct number *);
void insertatuserchoice(struct number *, int);

int i,x,count=0,y;
   
void main()
{
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);
   
   printf("Enter the value where we want to insert new node in the Linked List = ");
   scanf("%d",&y);
   
   if(x<y)
   {   	
   	printf("\nInsertion is not possible at given place in the linked list");
	exit(0);   	
   }
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   printf("The output before insertion are = ");
   display(home);  
   
   insertatuserchoice(home,y);
   printf("The output after insertion are = ");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;   
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{   
   for(link=home2;link!=NULL;link=link->ptr)   
    {        
        printf("%d ",link->num);			
    }   
}

void insertatuserchoice(struct number *home4, int k)
{
   	link=home4;
   	
   	newnode= (struct number*)malloc(sizeof(struct number));
	printf("\nEnter Integer Numeric Value for new node to insert at given place = ");       
        scanf("%d",&newnode->num);
        newnode->ptr=NULL;
	 
	if(count>=k)
	{  
		for(i=1;i<k;i++)
		{
			link=link->ptr;
		}
		newnode->ptr=link->ptr;	
		link->ptr=newnode;	   
	}
}
Output :
Enter the value for nodes in Linked List = 5
Enter the value where we want to insert new node in the Linked List = 2
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
Enter Integer Numeric Value = 50
The output before insertion are = 10 20 30 40 50
Enter Integer Numeric Value for new node to insert at given place = 100
The output after insertion are = 10 20 100 30 40 50
Example : A data structure program using C to delete a node from the beginning of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link;
	
void create(struct number *);
void display(struct number *);
void delatbeg(struct number *);

int i,x,count=0;
   
void main()
{
   printf("Enter the Node value for a Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   
   printf("\nThe Outputs before deletion are = \n");
   display(home);
   
   delatbeg(home);
   
   printf("\n\nThe Outputs after deletion from beginning are = \n");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;
   struct number *newnode;
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{
   for(link=home2;link!=NULL;link=link->ptr)   
    {
       printf("%d ",link->num);	
    }      
}

void delatbeg(struct number *home3)
{
   link=home3;
   
   home=home->ptr;
   free(link); 
        
}

Output :
Enter the Node value for a Linked List = 3
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30

The Outputs before deletion are =
10 20 30

The Outputs after deletion from beginning are =
20 30
Example : A data structure program using C to delete a node from the middle of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link;
	
void create(struct number *);
void display(struct number *);
void counting(struct number *);
void delatmid(struct number *);

int i,x,count=0,mid;
   
void main()
{
   printf("Enter the Node value for a Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);   
   printf("\nThe Outputs before deletion are = \n");
   display(home);
   
   counting(home);
   
   delatmid(home);   
   printf("\n\nThe Outputs after deletion from beginning are = \n");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;
   struct number *newnode;   	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{
   for(link=home2;link!=NULL;link=link->ptr)   
    {
       printf("%d ",link->num);	
    }      
}

void counting(struct number *home3)
{   
   for(link=home3;link!=NULL;link=link->ptr)   
    {        
        count=count+1;		
    } 
	mid=count/2;	  
}

void delatmid(struct number *home4)
{
   link=home4;
   
   if(x%2!=0)
   {
	  for(i=1;i<mid;i++)
	    {
	   	link=link->ptr;
	    }
   }
   else
   {
   	  for(i=1;i<mid-1;i++)
	    {
	   	link=link->ptr;
	    }
   }    
   home4=link->ptr;
   link->ptr=home4->ptr;
   
   free(home4);         
}

Output :
Enter the Node value for a Linked List = 5
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
Enter Integer Numeric Value = 50

The Outputs before deletion are =
10 20 30 40 50

The Outputs after deletion from beginning are =
10 20 40 50
Example : A data structure program using C to delete a node from the last/end of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h>
#include<stdlib.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link;
	
void create(struct number *);
void display(struct number *);
void delatend(struct number *);

int i,x,count=0,mid;
   
void main()
{
   printf("Enter the Node value for a Linked List = ");
   scanf("%d",&x);
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);   
   printf("\nThe Outputs before deletion are = \n");
   display(home);   
   
   
   delatend(home);   
   printf("\n\nThe Outputs after deletion from the end is = \n");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;
   struct number *newnode;   	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{
   for(link=home2;link!=NULL;link=link->ptr)   
    {
       printf("%d ",link->num);	
    }      
}

void delatend(struct number *home3)
{
      
   if(home3==NULL)
   {
	  printf("Deletion not Possible");
	  exit(0);
   }
   else
   {
   	  while(home3->ptr->ptr!=NULL)
   	  {
   	  	home3=home3->ptr;
	  }
   }    
   link=home3->ptr;
   home3->ptr=NULL;
   
   free(link);         
}
Output :
Enter the Node value for a Linked List = 4
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40

The Outputs before deletion are =
10 20 30 40

The Outputs after deletion from beginning are =
10 20 30
Example : A data structure program using C to delete a node from the customized/user’s given location of an existing  linked list using function.
#include <stdio.h>  
#include <conio.h> 
#include <stdlib.h>  
#include <malloc.h>

struct number
{
  int num;    
  struct number *ptr;  
};
   
struct number *home, *link, *newnode;
	
void create(struct number *);
void display(struct number *);
void delatuserchoice(struct number *, int);

int i,x,y;
   
void main()
{
   printf("Enter the value for nodes in Linked List = ");
   scanf("%d",&x);
   
   printf("Enter the value from where we want to delete the node from the Linked List = ");
   scanf("%d",&y);
   
   if(x<y)
   {   	
   	printf("\nDeletion is not possible at given place in the linked list");
	exit(0);   	
   }
   
   home= (struct number*)malloc(sizeof(struct number));
   printf("Enter Integer Numeric Value = ");
   scanf("%d",&home->num);
   home->ptr=NULL;
   
   create(home);
   printf("The output before deletion are = ");
   display(home);  
   
   delatuserchoice(home,y);
   printf("\n\nThe output after deletion are = ");
   display(home);
   
   getch();
}

void create(struct number *home1)
{
   link=home1;   
   int i;	   
   for(i=1;i<x;i++)
   {
     newnode= (struct number*)malloc(sizeof(struct number));
     printf("Enter Integer Numeric Value = ");
     scanf("%d",&newnode->num);
     newnode->ptr=NULL;
			
     link->ptr=newnode;
     link=link->ptr;			
   }
}
   
void display(struct number *home2)
{   
   for(link=home2;link!=NULL;link=link->ptr)   
    {        
        printf("%d ",link->num);			
    }   
}

void delatuserchoice(struct number *home3, int k)
{ 	
	{  
		for(i=1;i<k-1;i++)
		{
		    home3=home3->ptr;
		}			
		link=home3->ptr;
		home3->ptr=link->ptr;
		free(link);	   
	}
}

Output :
Enter the value for nodes in Linked List = 5
Enter the value from where we want to delete the node from the Linked List = 2
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
Enter Integer Numeric Value = 50
The output before deletion are = 10 20 30 40 50

The output after deletion are = 10 30 40 50

 264 total views,  3 views today


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.