SlideShare a Scribd company logo
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

PDF
C program
Komal Singh
 
DOC
Final ds record
Ganisius Ganish
 
DOC
Sorting programs
Varun Garg
 
DOCX
Data Structures Using C Practical File
Rahul Chugh
 
DOCX
Shortened Linked List in C programming easy to learn for exam
ssuser80a5aa
 
DOCX
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
PDF
Data Structures Practical File
Harjinder Singh
 
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 
C program
Komal Singh
 
Final ds record
Ganisius Ganish
 
Sorting programs
Varun Garg
 
Data Structures Using C Practical File
Rahul Chugh
 
Shortened Linked List in C programming easy to learn for exam
ssuser80a5aa
 
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
Data Structures Practical File
Harjinder Singh
 
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 

Similar to Singly linked list.pptx (20)

DOCX
DataStructures notes
Lakshmi Sarvani Videla
 
PPTX
Double linked list
Sayantan Sur
 
DOC
C basics
MSc CST
 
PPTX
Circular linked list
Sayantan Sur
 
PPTX
Single linked list
Sayantan Sur
 
DOC
Basic c programs updated on 31.8.2020
vrgokila
 
PDF
program on string in java Lab file 2 (3-year)
Ankit Gupta
 
DOCX
DS Code (CWH).docx
KamalSaini561034
 
PPTX
Singly linked list program in data structure - Vtech
Vtech Academy of Computers
 
PDF
c programming
Arun Umrao
 
PDF
Data structures lab manual
Syed Mustafa
 
DOCX
Implement of c &amp; its coding programming by sarmad baloch
Sarmad Baloch
 
PDF
DSC program.pdf
Prof. Dr. K. Adisesha
 
PPTX
Chapter5.pptx
dhanajimirajkar1
 
DOCX
Lab Week 2 Game Programming.docx
teyaj1
 
DOCX
C lab manaual
manoj11manu
 
PPTX
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
kncetaruna
 
DOC
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
DOCX
Cpds lab
praveennallavelly08
 
DataStructures notes
Lakshmi Sarvani Videla
 
Double linked list
Sayantan Sur
 
C basics
MSc CST
 
Circular linked list
Sayantan Sur
 
Single linked list
Sayantan Sur
 
Basic c programs updated on 31.8.2020
vrgokila
 
program on string in java Lab file 2 (3-year)
Ankit Gupta
 
DS Code (CWH).docx
KamalSaini561034
 
Singly linked list program in data structure - Vtech
Vtech Academy of Computers
 
c programming
Arun Umrao
 
Data structures lab manual
Syed Mustafa
 
Implement of c &amp; its coding programming by sarmad baloch
Sarmad Baloch
 
DSC program.pdf
Prof. Dr. K. Adisesha
 
Chapter5.pptx
dhanajimirajkar1
 
Lab Week 2 Game Programming.docx
teyaj1
 
C lab manaual
manoj11manu
 
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
kncetaruna
 
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
Ad

More from Santhiya S (8)

PPTX
Binary search tree.pptx
Santhiya S
 
PPTX
Expression tree conversion.pptx
Santhiya S
 
PPTX
Queue implementation using Linked list.pptx
Santhiya S
 
PPTX
Queue implementation using Array.pptx
Santhiya S
 
PPTX
Stack implementation using Array.pptx
Santhiya S
 
PPTX
Circular linked list.pptx
Santhiya S
 
PPTX
Doubly linked list.pptx
Santhiya S
 
PPTX
Linked list memory allocation and its types.pptx
Santhiya S
 
Binary search tree.pptx
Santhiya S
 
Expression tree conversion.pptx
Santhiya S
 
Queue implementation using Linked list.pptx
Santhiya S
 
Queue implementation using Array.pptx
Santhiya S
 
Stack implementation using Array.pptx
Santhiya S
 
Circular linked list.pptx
Santhiya S
 
Doubly linked list.pptx
Santhiya S
 
Linked list memory allocation and its types.pptx
Santhiya S
 
Ad

Recently uploaded (20)

PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PPTX
quantum computing transition from classical mechanics.pptx
gvlbcy
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Information Retrieval and Extraction - Module 7
premSankar19
 
quantum computing transition from classical mechanics.pptx
gvlbcy
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
Inventory management chapter in automation and robotics.
atisht0104
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 

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