Capitol Tech U Doctoral Presentation - April 2024.pptx
Bcsl 033 data and file structures lab s3-3
1. S3-3
Write a program in ‘C’ language to accept a singly linked list of integers as
input.
Now, sort the elements of the list in ascending order.
Then, accept an integer as input. Insert this integer into the singly
linked list at the appropriate position.
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
struct student *next;
}*stud,*first;
/*******creation of list********/
void list_create(struct student *s1)
{
printf("enter roll number-1 to stop");
scanf("%d",&s1->rollno);
if(s1->rollno!=-1)
{
s1->next=(struct student*)malloc(sizeof(struct student));
list_create(s1->next);
}
else s1->next=NULL;
return;
}
void display_list(struct student *s1)
{
if(s1->next!=NULL)
{
printf("%dn",s1->rollno);
display_list(s1->next);
}
}
void sort_list(struct student *s1)
{
struct student *temp,*t,*t1;
if(s1->next==NULL)
return;
t=s1;
while(t)
{
t1=t->next;
while(t1->next!=NULL)
{
if(t->rollno>t1->rollno)
{
temp->rollno=t->rollno;
t->rollno=t1->rollno;
t1->rollno=temp->rollno;
}
t1=t1->next;
}
Page 1