SlideShare a Scribd company logo
1 of 3
Download to read offline
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
S3-3
t=t->next;
}
}
/**inserting an element to list*/
void insert_element(struct student *s1)
{ int r; struct student* temp,*prev;
printf("enter rollnumber to insert");
scanf("%d",&r);
if(r < s1->rollno)
{
temp=(struct student*)malloc(sizeof(struct student));
temp->rollno=r;
temp->next=s1;
first=temp; return;
}
while(s1->next)
{
if(s1->rollno <r)
{
prev=s1;
s1=s1->next;
}
else
{
temp=(struct student*)malloc(sizeof(struct student));
temp->rollno=r;
temp->next=prev->next;
prev->next=temp;
break;
}
}
/*to insert after last node*/
if(s1->next==NULL && r>s1->rollno)
{
temp=(struct student*)malloc(sizeof(struct student));
temp->rollno=r;
temp->next=prev->next;
prev->next=temp;
}
}
main()
{
clrscr();
first=(struct student*)malloc(sizeof(struct student));
stud=(struct student*)malloc(sizeof(struct student));
first=stud;// first->next=NULL;
list_create(stud);
display_list(stud);
printf("nSorted listn");
sort_list(stud);
display_list(stud);
insert_element(stud);
Page 2
S3-3
display_list(first);
getch();
return;
}
Page 3

More Related Content

What's hot

What's hot (20)

Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
 
Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3
 
week-1x
week-1xweek-1x
week-1x
 
week-10x
week-10xweek-10x
week-10x
 
week-11x
week-11xweek-11x
week-11x
 
SATySFiのこれからの課題たち
SATySFiのこれからの課題たちSATySFiのこれからの課題たち
SATySFiのこれからの課題たち
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Dti2143 lab sheet 9
Dti2143 lab sheet 9Dti2143 lab sheet 9
Dti2143 lab sheet 9
 
week-4x
week-4xweek-4x
week-4x
 
Computer programing w
Computer programing wComputer programing w
Computer programing w
 
Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4
 
One dimensional operation of Array in C- language
One dimensional operation of Array in C- language One dimensional operation of Array in C- language
One dimensional operation of Array in C- language
 
C program to add two numbers
C program to add two numbers C program to add two numbers
C program to add two numbers
 
Linked list2
Linked list2Linked list2
Linked list2
 
Ffffffffffff
FfffffffffffFfffffffffff
Ffffffffffff
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 

Similar to Bcsl 033 data and file structures lab s3-3

Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptxSanthiya S
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solutionAnimesh Chaturvedi
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdfAsrinath1
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxclarkjanyce
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project FileDeyvessh kumar
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxArvindShukla81
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSGautham Rajesh
 
Write a program that prompts the user to enter a positive integer and.pdf
Write a program that prompts the user to enter a positive integer and.pdfWrite a program that prompts the user to enter a positive integer and.pdf
Write a program that prompts the user to enter a positive integer and.pdffeetshoemart
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxjeyel85227
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4YOGESH SINGH
 

Similar to Bcsl 033 data and file structures lab s3-3 (20)

Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docx
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptx
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
Write a program that prompts the user to enter a positive integer and.pdf
Write a program that prompts the user to enter a positive integer and.pdfWrite a program that prompts the user to enter a positive integer and.pdf
Write a program that prompts the user to enter a positive integer and.pdf
 
Struct examples
Struct examplesStruct examples
Struct examples
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptx
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 

More from Dr. Loganathan R

Ch 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdfCh 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdfDr. Loganathan R
 
IoT Sensing and Actuation.pdf
 IoT Sensing and Actuation.pdf IoT Sensing and Actuation.pdf
IoT Sensing and Actuation.pdfDr. Loganathan R
 
Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Dr. Loganathan R
 
Introduction to Information Security
Introduction to Information SecurityIntroduction to Information Security
Introduction to Information SecurityDr. Loganathan R
 
Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...Dr. Loganathan R
 
Session 9 4 alp to display the current system time using dos int 21 h
Session 9  4 alp to display the current system time using dos int 21 hSession 9  4 alp to display the current system time using dos int 21 h
Session 9 4 alp to display the current system time using dos int 21 hDr. Loganathan R
 
Session 9 1 alp to compute a grade using procedures
Session 9   1 alp to compute a grade using proceduresSession 9   1 alp to compute a grade using procedures
Session 9 1 alp to compute a grade using proceduresDr. Loganathan R
 
IGNOU Assembly Language Programming
IGNOU Assembly Language ProgrammingIGNOU Assembly Language Programming
IGNOU Assembly Language ProgrammingDr. Loganathan R
 
Requirement engineering process
Requirement engineering processRequirement engineering process
Requirement engineering processDr. Loganathan R
 
MCS 012 computer organisation and assembly language programming assignment…
MCS 012 computer organisation and assembly language programming assignment…MCS 012 computer organisation and assembly language programming assignment…
MCS 012 computer organisation and assembly language programming assignment…Dr. Loganathan R
 
Software engineering critical systems
Software engineering   critical systemsSoftware engineering   critical systems
Software engineering critical systemsDr. Loganathan R
 
Software engineering socio-technical systems
Software engineering   socio-technical systemsSoftware engineering   socio-technical systems
Software engineering socio-technical systemsDr. Loganathan R
 
Software engineering introduction
Software engineering   introductionSoftware engineering   introduction
Software engineering introductionDr. Loganathan R
 

More from Dr. Loganathan R (17)

Ch 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdfCh 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdf
 
IoT Sensing and Actuation.pdf
 IoT Sensing and Actuation.pdf IoT Sensing and Actuation.pdf
IoT Sensing and Actuation.pdf
 
Ch 4 Emergence of IoT.pdf
Ch 4 Emergence of IoT.pdfCh 4 Emergence of IoT.pdf
Ch 4 Emergence of IoT.pdf
 
Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2
 
Introduction to Information Security
Introduction to Information SecurityIntroduction to Information Security
Introduction to Information Security
 
Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...
 
Session 9 4 alp to display the current system time using dos int 21 h
Session 9  4 alp to display the current system time using dos int 21 hSession 9  4 alp to display the current system time using dos int 21 h
Session 9 4 alp to display the current system time using dos int 21 h
 
Session 9 1 alp to compute a grade using procedures
Session 9   1 alp to compute a grade using proceduresSession 9   1 alp to compute a grade using procedures
Session 9 1 alp to compute a grade using procedures
 
IGNOU Assembly Language Programming
IGNOU Assembly Language ProgrammingIGNOU Assembly Language Programming
IGNOU Assembly Language Programming
 
Requirement engineering process
Requirement engineering processRequirement engineering process
Requirement engineering process
 
MCS 012 computer organisation and assembly language programming assignment…
MCS 012 computer organisation and assembly language programming assignment…MCS 012 computer organisation and assembly language programming assignment…
MCS 012 computer organisation and assembly language programming assignment…
 
Software requirements
Software requirementsSoftware requirements
Software requirements
 
Software process
Software processSoftware process
Software process
 
Software engineering critical systems
Software engineering   critical systemsSoftware engineering   critical systems
Software engineering critical systems
 
Software engineering socio-technical systems
Software engineering   socio-technical systemsSoftware engineering   socio-technical systems
Software engineering socio-technical systems
 
Software engineering introduction
Software engineering   introductionSoftware engineering   introduction
Software engineering introduction
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
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
  • 2. S3-3 t=t->next; } } /**inserting an element to list*/ void insert_element(struct student *s1) { int r; struct student* temp,*prev; printf("enter rollnumber to insert"); scanf("%d",&r); if(r < s1->rollno) { temp=(struct student*)malloc(sizeof(struct student)); temp->rollno=r; temp->next=s1; first=temp; return; } while(s1->next) { if(s1->rollno <r) { prev=s1; s1=s1->next; } else { temp=(struct student*)malloc(sizeof(struct student)); temp->rollno=r; temp->next=prev->next; prev->next=temp; break; } } /*to insert after last node*/ if(s1->next==NULL && r>s1->rollno) { temp=(struct student*)malloc(sizeof(struct student)); temp->rollno=r; temp->next=prev->next; prev->next=temp; } } main() { clrscr(); first=(struct student*)malloc(sizeof(struct student)); stud=(struct student*)malloc(sizeof(struct student)); first=stud;// first->next=NULL; list_create(stud); display_list(stud); printf("nSorted listn"); sort_list(stud); display_list(stud); insert_element(stud); Page 2