Table of Contents hide
Example : Data structure Singly Linked List Examples/programs using C to create a single-inked list with five nodes to store five records with at least two student attributes and then display all the stored records using for/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;  
	
   //first 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;		
	
   //rest four Node creation code
   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/Print/Traverse 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

----------------  OR  ----------------

#include <stdio.h>  
#include <conio.h>  
#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *home, *link, *newnode;
void main()
{
   int i;  
	
   //first 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;		
	
   //rest four Node creation code
   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/Print/Traverse code
   printf("\nThe Students records are = \n");   
   for(link=home;link!=NULL;link=link->ptr)
   {
	printf("%d  %s\n",link->srollno,link->sname);
			
   }	
   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

----------------  OR  ----------------

#include <stdio.h>
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};

typedef struct student ss;   
ss *home, *link, *newnode;
void main()
{
   int i;  
	
   //first Node creation code
   home= (ss*)malloc(sizeof(ss));
   printf("Enter Roll No. and Student name = ");
   scanf("%d%s",&home->srollno,home->sname);
   home->ptr=NULL;		
	
   //rest four Node creation code
   link=home;
   for(i=1;i<5;i=i+1)
   {
	newnode= (ss*)malloc(sizeof(ss));
	printf("Enter Roll No. and Student name = ");
	scanf("%d%s",&newnode->srollno,newnode->sname);
	newnode->ptr=NULL;
		
	link->ptr=newnode;
	link=link->ptr;			
   }
	
   //Display/Print/Traverse 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 = 10 A
Enter Roll No. and Student name = 20 B
Enter Roll No. and Student name = 30 C
Enter Roll No. and Student name = 40 D
Enter Roll No. and Student name = 50 E

The Outputs are =
10  A
20  B
30  C
40  D
50  E
Example : Data structure Singly Linked List Examples/programs using C++ to create a single inked list with five nodes to store five records with at least two attributes of student and then display all the stored records.
# include <iostream>
using namespace std;  

class student
{
  public:  	
	  int srollno;
	  char sname[50];  
	  student *ptr;  
};

student *home, *link, *newnode = NULL;

int main()
{
   int i;  
	
   //first Node creation code
   home= new student();
   printf("Enter Roll No. and Student name = ");
   scanf("%d%s",&home->srollno,home->sname);
   home->ptr=NULL;		
	
   //rest four Node creation code
   link=home;
   for(i=1;i<5;i=i+1)
   {
	newnode= new 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/Print/Traverse code
   printf("\nThe Outputs are = \n");
   link=home;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }	
   
   return 0;
}

----------------  OR  ----------------

# include <iostream>
using namespace std;  

class student
{
  public:  	
	  int srollno;
	  char sname[50];  
	  student *ptr;  
};

student *home, *link, *newnode=NULL;

int main()
{
   int i;  
	
   //first Node creation code
   home= new student();
   cout<<"Enter Roll No. and Student name = ";
   cin>>home->srollno>>home->sname;
   home->ptr=NULL;		
	
   //rest four Node creation code
   link=home;
   for(i=1;i<5;i=i+1)
   {
	newnode= new student();
	cout<<"Enter Roll No. and Student name = ";
	cin>>newnode->srollno>>newnode->sname;
	newnode->ptr=NULL;
		
	link->ptr=newnode;
	link=link->ptr;			
   }
	
   //Display/Print/Traverse code
   cout<<endl<<"The Outputs are ="<<endl;
   link=home;
   while(link!=NULL)
   {
	cout<<link->srollno<<"  "<<link->sname<<endl;
	link=link->ptr;		
   }	
   
   return 0;
}

Output:
Enter Roll No. and Student name = 10 A
Enter Roll No. and Student name = 20 B
Enter Roll No. and Student name = 30 C
Enter Roll No. and Student name = 40 D
Enter Roll No. and Student name = 50 E

The Outputs are =
10  A
20  B
30  C
40  D
50  E
Example : Data structure Singly Linked List Examples/programs using C to create a single linked list with user-defined/given nodes and display all the records.
#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 : Data structure Singly Linked List Examples/programs using C to create a single linked list with nodes equal to the user’s given value and display all the records using a user-defined function (UDF) [Creating the Linked List].
#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 [Counting the 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 [Searching the Linked List].
#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 values from the existing singly linked list without using UDF.
#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
Example: A data structure program using C to Merge the created two linked list with their data and display the merge linked list data [Merging the Linked List]
.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *home1, *home2, *link, *newnode;
void main()
{
   int i;  
	
   //first Node creation code
   home1= (struct student*)malloc(sizeof(struct student));
   printf("Enter Roll No. and Student name for first Linked list = ");
   scanf("%d%s",&home1->srollno,home1->sname);
   home1->ptr=NULL;		
	
   //rest four Node creation code
   link=home1;
   for(i=1;i<5;i=i+1)
   {
	newnode= (struct student*)malloc(sizeof(struct student));
	 printf("Enter Roll No. and Student name for first Linked list = ");
	scanf("%d%s",&newnode->srollno,newnode->sname);
	newnode->ptr=NULL;
		
	link->ptr=newnode;
	link=link->ptr;			
   }
	
   //Display/Print/Traverse code
   printf("\nThe Outputs of first linked list befor Merging is = \n");
   link=home1;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }
   
   home2= (struct student*)malloc(sizeof(struct student));
    printf("Enter Roll No. and Student name for Second Linked list = ");
   scanf("%d%s",&home2->srollno,home2->sname);
   home2->ptr=NULL;	
   
   link=home2;
   for(i=1;i<5;i=i+1)
   {
	newnode= (struct student*)malloc(sizeof(struct student));
	 printf("Enter Roll No. and Student name for Second Linked list = ");
	scanf("%d%s",&newnode->srollno,newnode->sname);
	newnode->ptr=NULL;
		
	link->ptr=newnode;
	link=link->ptr;			
   }
   
    printf("\nThe Outputs of second linked list before Merging is = \n");
   link=home2;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }
   

    // Merging Code
   link=home1;
   while(link->ptr!=NULL)
   {	
	link=link->ptr;		
   }
   
   link->ptr=home2;
   
   printf("\nThe Outputs of Merged linked list is = \n");
   link=home1;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }
   	
   getch();
}

Output :
Enter Roll No. and Student name for first Linked list = 1 a
Enter Roll No. and Student name for first Linked list = 2 b
Enter Roll No. and Student name for first Linked list = 3 c
Enter Roll No. and Student name for first Linked list = 4 d
Enter Roll No. and Student name for first Linked list = 5 e

The Outputs of first linked list befor Merging is =
1  a
2  b
3  c
4  d
5  e

Enter Roll No. and Student name for Second Linked list =  6 f
Enter Roll No. and Student name for Second Linked list = 7 g
Enter Roll No. and Student name for Second Linked list = 8 h
Enter Roll No. and Student name for Second Linked list = 9 i
Enter Roll No. and Student name for Second Linked list = 10 j

The Outputs of second linked list before Merging is =
6  f
7  g
8  h
9  i
10  j

The Outputs of Merged linked list is =
1  a
2  b
3  c
4  d
5  e
6  f
7  g
8  h
9  i
10  j
Example: A data structure program using C to Split a single linked list into two separate linked list and display their data [Splitting the Linked List].
#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, *home1, *link, *newnode;
void main()
{
   int i;  
	
   //first 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;		
	
   //rest nine Node creation code
   link=home;
   for(i=1;i<10;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/Print/Traverse code
   printf("\nThe Output before splitting of linked list are = \n");
   link=home;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }
   
   //Splitting code
   link=home;
   int count=0;
   
   while(link!=NULL)
   {
	count=count+1;
	link=link->ptr;		
   }
   
   count=count/2;
   link=home;   
   for(i=1;i<count;i++)
   {
   	link=link->ptr;
   }

   home1=link->ptr;
   link->ptr=NULL;
   	
    printf("\nThe Output after splitting of first linked list are = \n");
    link=home;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }
   
   printf("\nThe Output after splitting of second linked list are = \n");
    link=home1;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }	
   	
   getch();
}

Output:
The Output before splitting of linked list are =
1  a
2  b
3  c
4  d
5  e
6  f
7  g
8  h
9  i
10  j

The Output after splitting of first linked list are =
1  a
2  b
3  c
4  d
5  e

The Output after splitting of second linked list are =
6  f
7  g
8  h
9  i
10  j
Example: A data structure program using C to Modify a single linked list specific values with other values. Display the modify data [Updating in Linked List].
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>
#include <string.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *home, *link, *newnode;
void main()
{
   int i;  
	
   //first 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;		
	
   //rest two Node creation code
   link=home;
   for(i=1;i<3;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/Print/Traverse code
   printf("\nThe Output of linkded list are = \n");
   link=home;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->srollno,link->sname);
	link=link->ptr;		
   }
   
   //Data Updating code
   int x1,x2;
   char stu1[50], stu2[50];
   
   printf("\nEnter the rollno and student name U want to modify = \n");
   scanf("%d%s",&x1,stu1);
   
   printf("\nNow Enter the rollno and student name for updation = \n");
   scanf("%d%s",&x2,stu2);       
   
   link=home;
   while(link!=NULL)
   {
	if(link->srollno==x1)
	{	
		link->srollno=x2;		
	}
	
	if(stricmp(link->sname,stu1)==0)
	{
		strcpy(link->sname,stu2);		
	}
	
	link=link->ptr;		
   }   
   
   printf("\nThe Output after Updation of linkded list 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 = 101 a
Enter Roll No. and Student name = 102 B
Enter Roll No. and Student name = 103 C

The Output of linked list are =
101  a
102  B
103  C

Enter the rollno and student name U want to modify =
101 C

Now Enter the rollno and student name for updation =
100 F

The Output after Updation of linkded list are =
100  a
102  B
103  F
Example: A data structure program in C to Reverse a single linked list value. Display it. [Reverse values in Linked List].
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct node
{
  int data;    
  struct node *ptr;  
};
   
struct node *head, *link, *temp, *newnode;
void main()
{
  int i, n;
	  
	printf("Enter the node number U want to create in Linked list for reversing\n");
	scanf("%d",&n);
	
 	//first Node creation code
 	head= (struct node*)malloc(sizeof(struct node));
 	printf("\nEnter %d values = \n",n);
 	scanf("%d",&head->data);
 	head->ptr=NULL;		
	
 	//rest four Node creation code
 	temp=head;
 	for(i=1;i<n;i=i+1)
 	{
		newnode= (struct node*)malloc(sizeof(struct node));		
		scanf("%d",&newnode->data);
		newnode->ptr=NULL;
	
		temp->ptr=newnode;
		temp=temp->ptr;			
 	}
	
 	//Display/Print/Traverse code
 	printf("\nThe Entered values are = \n");
 	temp=head;
 	while(temp!=NULL)
 	{
		printf("%d \n",temp->data);
		temp=temp->ptr;		
 	}
	 
 	temp=head;
 	link=NULL;
 	while(temp!=NULL)
	{
 		newnode=link;
 		link=temp;
 		temp=temp->ptr;
 		link->ptr=newnode;
	}		 
	head=link;
	 
	printf("\nThe Reverse Outputs are = \n");
 	temp=head;
 	while(temp!=NULL)
 	{
		printf("%d  ",temp->data);
		temp=temp->ptr;		
 	}
		 
  getch();
}

Output:
Enter the node number U want to create in Linked list for reversing
5

Enter 5 values =
62
5
400
1
90

The Entered values are =
62
5
400
1
90

The Reverse Outputs are =
90  1  400  5  62
Example: A data structure program in C to Sort the node data in Ascending Order. Display the sorted data. [Selection Sort in Single Linked List].
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct node
{
  int data;    
  struct node *ptr;  
};
   
struct node *head, *temp, *link, *newnode;
void main()
{
  int i,x, n;
	  
	printf("Enter the node number U want to create in Linked list for reversing\n");
	scanf("%d",&n);
	
 	//first Node creation code
 	head= (struct node*)malloc(sizeof(struct node));
 	printf("\nEnter %d values = \n",n);
 	scanf("%d",&head->data);
 	head->ptr=NULL;		
	
 	//rest four Node creation code
 	temp=head;
 	for(i=1;i<n;i=i+1)
 	{
		newnode= (struct node*)malloc(sizeof(struct node));		
		scanf("%d",&newnode->data);
		newnode->ptr=NULL;
	
		temp->ptr=newnode;
		temp=temp->ptr;			
 	}
	
 	//Display/Print/Traverse code
 	printf("\nThe Output before Sorting are = \n");
 	temp=head;
 	while(temp!=NULL)
 	{
		printf("%d \n",temp->data);
		temp=temp->ptr;		
 	}
 	
 	for(temp=head;temp!=NULL;temp=temp->ptr)
 		{
 			for(link=temp->ptr;link!=NULL;link=link->ptr)
 			{
 				if(temp->data>link->data)
				{
					x=link->data;
					link->data=temp->data;
					temp->data=x;
				}
			}
		}
 	
 	//Display/Print/Traverse code
 	printf("\nThe Output after Sorting are = \n"); 	
 	temp=head;
 	while(temp!=NULL)
 	{
		printf("%d \n",temp->data);
		temp=temp->ptr;		
 	}
		 
  getch();
}

Output:
Enter the node number U want to create in Linked list for reversing
5

Enter 5 values =
200
50
1
600
10

The Output before Sorting are =
200
50
1
600
10

The Output after Sorting are =
1
10
50
200
600

Loading


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.