SlideShare a Scribd company logo
1 of 10
DATA STRUCTURES
Santhiya S
Assistant Professor
Department of AI
Kongu Engineering College
Code implementation of Singly Linked List
INSERTING A VALUE IN A LINKED LIST
There are three ways of inserting a node in a linked list they are:
• Insert at the beginning
• Insert at the middle
• Insert at the end
#include<stdio.h>
#include<stdlib.h>
void display();
int length();
void insert_begin();
void insert_middle();
void insert_end();
void delete();
void delete_end();
struct node
{
int data;
struct node* next;
};
struct node* head;
void main()
{
int n,i=0;
printf("enter number of element to insert:");
scanf("%d",&n);
while(i<n)
{
insert_begin ();
i++;
}
while(i<n)
{
insert_end();
i++;
}
insert_middle();
delete();
delete_end();
display();
length();
}
void display()
{
struct node* p;
p=head;
while(p!=NULL)
{
printf("%d->%pn",p->data,p->next);
p=p->next;
}
}
int length()
{
int count=0;
struct node* temp;
temp=root;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
void insert_begin()
{
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter datan");
scanf("%d", &newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}
*newnode
100
100
*newnode
35 NULL
head
if NULL==NULL True
100
200
200
*newnode
45 NULL
if 100==NULL False,
else block will be
executed
100
head
200
300
55 NULL
300
*newnode
if 200==NULL False
else block will be
executed
200
head
300
void insert_end()
{
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter datan");
scanf("%d", &newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
struct node* temp;
temp=head;
while(temp-> next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
*newnode
100
100
*newnode
35 NULL
temp
if NULL==NULL True
100
200
200
*newnode
45 NULL
if 100==NULL False,
else block will be
executed
100
head
300
55 NULL
300
*newnode
if 100==NULL False
else block will be
executed
200 300
while NULL!=NULL
False
while 200!=NULL True
temp
200
while NULL!=NULL
False
INSERT AT THE MIDDLE
void insert_middle()
{
struct node *newnode, *p;
int loc,len, i=1;
printf(“Enter location”);
scanf(“%d”,&loc);
len=length();
if(loc>len)
{
printf(“Invlaid Location”);
printf(“Currently list is having %d node”,len);
}
else
{
p=head;
while(i<loc)
{
p=p->next;
i++;
}
}
newnode = (struct node *) malloc (sizeof(struct
node));
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->next=p->next;(right side)
p->next=newnode;(left side)
}
94 Nul
l
500
head
27 200
100
45 300
200
20 500
300
len
*p
1
loc
*newnode
i
3
loc
3>4
if
100
100
*p
while(1<3)
200
*p
2
while(2<3)
300
*p
3
while(3<3)
False
450
450
*newnode
35
35 NU
LL
35 NU
LL
450
35 500
450
450
head
27 200
100
45 300
200
100
20 450
300
35 500
450
94 Nul
l
500
4
DELETING A VALUE IN A LINKED LIST
There are three ways of deleting a node in a linked list they are:
1) Delete at the beginning
2) Delete at the middle
3) Delete at the end
DELETE AT THE BEGINING
void delete()
{
struct node *temp;
int loc, len;
printf(“Enter location”);
scanf(“%d”,&loc)
len=length();
if(loc>len)||(head==NULL)
{
printf(“Invalid Location or empty list);
}
else if(loc==1)
{
temp=head;
head=temp->next; (left side connection lost)
temp->next=NULL; (right side connection lost)
free(temp);
}
temp loc len
1 3
if(1>3)||(100==NULL)
False
else if(1==1) True
head
27 200
100
45 300
200
100
20 NU
LL
300
100
temp temp->next=200
200 27 NULL
DELETE AT THE BEGINING
DELETE AT THE MIDDLE
else
{
struct node *p,*q;
p=head;
int i=1;
while(i<loc-1)
{
p=p->next;
i++;
}
q=p->next;
p->next=q->next;(left side connection lost)
q->next=NULL; (right side connection lost)
free(q);
}
}
head
27 200
100
45 300
200
100
20 450
300
35 500
450
94 Null
500
p
*q
100
1
i
while(1<2)True
100
*p
p
200
while(2<2)False
q
300
45 450
2
P->next=300
q->next=450
20 0
DELETE AT THE END
void delete_end();
{
struct node*p,*q;
p=head;
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
free(p);
}
27 200
100 200
100
300
35 500
450
94 Null
500
p
100
q
100
45 300 20 450
200!=NULL True
p
200
300!=NULL True
450!=NULL True
q
200
p
300
500!=NULL True
q
300
p
450
NULL!=NULL False
q
450
p
500
35 NULL

More Related Content

Similar to Singly linked list.pptx

C basics
C basicsC basics
C basics
MSc CST
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 

Similar to Singly linked list.pptx (20)

Vcs29
Vcs29Vcs29
Vcs29
 
Double linked list
Double linked listDouble linked list
Double linked list
 
C basics
C basicsC basics
C basics
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Single linked list
Single linked listSingle linked list
Single linked list
 
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
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
Singly linked list program in data structure - Vtech
Singly linked list program in data structure - VtechSingly linked list program in data structure - Vtech
Singly linked list program in data structure - Vtech
 
c programming
c programmingc programming
c programming
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad baloch
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
M 0 1 2 3 4 5 6 7 0.pdf
 M  0  1  2  3  4  5  6  7    0.pdf M  0  1  2  3  4  5  6  7    0.pdf
M 0 1 2 3 4 5 6 7 0.pdf
 
งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์
 

More from Santhiya S

More from Santhiya S (8)

Binary search tree.pptx
Binary search tree.pptxBinary search tree.pptx
Binary search tree.pptx
 
Expression tree conversion.pptx
Expression tree conversion.pptxExpression tree conversion.pptx
Expression tree conversion.pptx
 
Queue implementation using Linked list.pptx
Queue implementation using Linked list.pptxQueue implementation using Linked list.pptx
Queue implementation using Linked list.pptx
 
Queue implementation using Array.pptx
Queue implementation using Array.pptxQueue implementation using Array.pptx
Queue implementation using Array.pptx
 
Stack implementation using Array.pptx
Stack implementation using Array.pptxStack implementation using Array.pptx
Stack implementation using Array.pptx
 
Circular linked list.pptx
Circular linked list.pptxCircular linked list.pptx
Circular linked list.pptx
 
Doubly linked list.pptx
Doubly linked list.pptxDoubly linked list.pptx
Doubly linked list.pptx
 
Linked list memory allocation and its types.pptx
Linked list memory allocation and its types.pptxLinked list memory allocation and its types.pptx
Linked list memory allocation and its types.pptx
 

Recently uploaded

1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 

Singly linked list.pptx

  • 1. DATA STRUCTURES Santhiya S Assistant Professor Department of AI Kongu Engineering College Code implementation of Singly Linked List
  • 2. INSERTING A VALUE IN A LINKED LIST There are three ways of inserting a node in a linked list they are: • Insert at the beginning • Insert at the middle • Insert at the end
  • 3. #include<stdio.h> #include<stdlib.h> void display(); int length(); void insert_begin(); void insert_middle(); void insert_end(); void delete(); void delete_end(); struct node { int data; struct node* next; }; struct node* head; void main() { int n,i=0; printf("enter number of element to insert:"); scanf("%d",&n); while(i<n) { insert_begin (); i++; } while(i<n) { insert_end(); i++; } insert_middle(); delete(); delete_end(); display(); length(); } void display() { struct node* p; p=head; while(p!=NULL) { printf("%d->%pn",p->data,p->next); p=p->next; } } int length() { int count=0; struct node* temp; temp=root; while(temp!=NULL) { count++; temp=temp->next; } return count; }
  • 4. void insert_begin() { struct node* newnode; newnode=(struct node*)malloc(sizeof(struct node)); printf("enter datan"); scanf("%d", &newnode->data); newnode->next=NULL; if(head==NULL) { head=newnode; } else { newnode->next=head; head=newnode; } } *newnode 100 100 *newnode 35 NULL head if NULL==NULL True 100 200 200 *newnode 45 NULL if 100==NULL False, else block will be executed 100 head 200 300 55 NULL 300 *newnode if 200==NULL False else block will be executed 200 head 300
  • 5. void insert_end() { struct node* newnode; newnode=(struct node*)malloc(sizeof(struct node)); printf("enter datan"); scanf("%d", &newnode->data); newnode->next=NULL; if(head==NULL) { head=newnode; } else { struct node* temp; temp=head; while(temp-> next!=NULL) { temp=temp->next; } temp->next=newnode; } } *newnode 100 100 *newnode 35 NULL temp if NULL==NULL True 100 200 200 *newnode 45 NULL if 100==NULL False, else block will be executed 100 head 300 55 NULL 300 *newnode if 100==NULL False else block will be executed 200 300 while NULL!=NULL False while 200!=NULL True temp 200 while NULL!=NULL False
  • 6. INSERT AT THE MIDDLE void insert_middle() { struct node *newnode, *p; int loc,len, i=1; printf(“Enter location”); scanf(“%d”,&loc); len=length(); if(loc>len) { printf(“Invlaid Location”); printf(“Currently list is having %d node”,len); } else { p=head; while(i<loc) { p=p->next; i++; } } newnode = (struct node *) malloc (sizeof(struct node)); scanf("%d",&newnode->data); newnode->next=NULL; newnode->next=p->next;(right side) p->next=newnode;(left side) } 94 Nul l 500 head 27 200 100 45 300 200 20 500 300 len *p 1 loc *newnode i 3 loc 3>4 if 100 100 *p while(1<3) 200 *p 2 while(2<3) 300 *p 3 while(3<3) False 450 450 *newnode 35 35 NU LL 35 NU LL 450 35 500 450 450 head 27 200 100 45 300 200 100 20 450 300 35 500 450 94 Nul l 500 4
  • 7. DELETING A VALUE IN A LINKED LIST There are three ways of deleting a node in a linked list they are: 1) Delete at the beginning 2) Delete at the middle 3) Delete at the end
  • 8. DELETE AT THE BEGINING void delete() { struct node *temp; int loc, len; printf(“Enter location”); scanf(“%d”,&loc) len=length(); if(loc>len)||(head==NULL) { printf(“Invalid Location or empty list); } else if(loc==1) { temp=head; head=temp->next; (left side connection lost) temp->next=NULL; (right side connection lost) free(temp); } temp loc len 1 3 if(1>3)||(100==NULL) False else if(1==1) True head 27 200 100 45 300 200 100 20 NU LL 300 100 temp temp->next=200 200 27 NULL DELETE AT THE BEGINING
  • 9. DELETE AT THE MIDDLE else { struct node *p,*q; p=head; int i=1; while(i<loc-1) { p=p->next; i++; } q=p->next; p->next=q->next;(left side connection lost) q->next=NULL; (right side connection lost) free(q); } } head 27 200 100 45 300 200 100 20 450 300 35 500 450 94 Null 500 p *q 100 1 i while(1<2)True 100 *p p 200 while(2<2)False q 300 45 450 2 P->next=300 q->next=450 20 0
  • 10. DELETE AT THE END void delete_end(); { struct node*p,*q; p=head; while(p->next!=NULL) { q=p; p=p->next; } q->next=NULL; free(p); } 27 200 100 200 100 300 35 500 450 94 Null 500 p 100 q 100 45 300 20 450 200!=NULL True p 200 300!=NULL True 450!=NULL True q 200 p 300 500!=NULL True q 300 p 450 NULL!=NULL False q 450 p 500 35 NULL