Download the ppt for animation of Singly linked list insertion and deletion
Note: if you have downloaded it in .pdf, convert it to .pptx and press F5 for animation.
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