3.1 Single Linked List
3.1 Single Linked List
Linked list
A linked list is a linear data structure where
data is stored in the form of a node.
Traversal ( Display )
Creation of list
struct slist
{
int info;
slist *next;
};
typedef struct slist* node;
INSERTION AT FRONT END
Insertion at the Front end
Insertion at the Front end
1. create a new node ( say temp) containing the
information to be inserted.
2. slist newnode;
3. newnode->info=data;
4. newnode->next=NULL;
5. If the list is empty i.e. head = NULL then make
the new node as the head node in the list
6. head=newnode;
Insertion at the Front end
7. If the list is not empty then
- Store the address of head node in the next part
of the new node and
-Make the new node as the head node
newnode->next=head;
head = newnode;
Insertion at the Front end
1. create a new node ( say temp) containing
the information to be inserted.
2. node temp;
3. temp->info=data;
4. temp->next=NULL;
Insertion at the Front end
5. If the list is empty i.e. head = NULL then
make the new node as the head node in the
list
Insertion at the Front end
5. If the list is empty i.e. head = NULL then
make the new node as the head node in the
list
6. head=temp; head
Insertion at the Front end
7. If the list is not empty then
- Store the address of head node in the next
part of the new node
temp->next=head
Insertion at the Front end
curn-> next is 200 which is not equal to NULL so curn moves to next node
Insertion at the Rear end
While (curn->next!=NULL)
Curn=curn->next;
curn-> next is 300 which is not equal to NULL so curn moves to next node
Insertion at the Rear end
While (curn->next!=NULL)
Curn=curn->next;
2. Head=head->next
DELETION AT REAR END
Deletion at Rear end
Deletion at Rear end
void deletion_rear(slist * head)
{
slist * curn, *prev;
if(head==NULL) // CASE-1
printf(“LIST IS EMPTY");
else // CASE-2 & 3
{
curn=head;
prev=curn;
while(curn->next !=NULL)
{
prev=curn;
curn=curn->next ;
}
printf("\nDeleted element is %d ",curn->info) ;
delete curn;
prev->next =NULL;
}
}
Deletion at rear end
CASE-1
1. if(head==NULL)
printf(“List is empty”);
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
3. while(curn->next !=NULL)
4. {
5. prev=curn;
6. curn=curn->next ;
7. }
Curn-> next is 200 which is not NULL so we enter into while loop and
curn moves to next node
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
3. while(curn->next !=NULL)
4. {
5. prev=curn;
6. curn=curn->next ;
7. }
Curn-> next is 300 which is not NULL so curn moves to next node
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
3. while(curn->next !=NULL)
4. {
5. prev=curn;
6. curn=curn->next ;
7. }
printf(“List is empty”);
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 100 its not equal to NULL, so curn->info that’s 10 is displayed and
curn moves to next node
10
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 200 its not equal to NULL, so curn->info that’s 20 is displayed and
curn moves to next node
10 20
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 300 its not equal to NULL, so curn->info that’s 30 is displayed and
curn moves to next node
10 20 30
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Now Curn is NULL so while is terminated.
10 20 30