2. LINKED LIST
Linked list is linear data structure which is collection of
zero or more nodes where each node has two fields.
root
1000 2000 3000
Node
10 2000 20 3000
30
NULL
Information Link
1000
3. REPRESENTATION OF SINGLY
LINKED LIST IN C
A structure is a collection of one or more fields which
are of same or different types.
The nodes in a linked list are self referential structures.
Self referential structure is a structure which has at least
one field which is pointer to same structure
Ex:
struct node
{
int data;
struct node *link;
};
struct node *root=NULL;
4. TYPES OF LINKED LIST
1.Singly linked list.
2.Doubly linked list.
3.Circular singly linked lists.
4.Circular doubly linked lists.
5. SINGLY LINKED LIST
It is a collection of zero or more nodes where each
node has two or more fields and only one link field
which contains address of the next node.
NULL
root
5412 10 NULL
5412
1000 20 NULL
1000
8120 40 NULL
2748
30 NULL
8120
2748
Pictorial representation of singly linked list
Operations performed on singly linked
list are :
1.Inserting a node into the list
2.Deleting node from the list
3.Searching a key in a list
6. 1.INSERTING A NODE INTO THE LIST
A. Inserting a node at rear end.
void insert rear-end()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf(“ Enter Node Datan”);
scanf(“%d”,&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp; 1000 2000 3000
}
else
{
struct node *p; root
p=root;
while(p->link!=NULL) p
4000
{
p=p->link;
}
p->link=temp;
temp
}
10 2000 20 3000
40 NULL
30 NULL
1000
4000
1000
7. B. Inserting a node at front end.
void insert rear-end()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf(“ Enter Node Datan”);
scanf(“%d”,&temp->data);
temp->link=NULL;
if(root==NULL)
{ 1000 2000 3000
root=temp;
}
else root
{ 500
temp->link=root;
root=temp;
} temp
}
10 2000 20 3000 30 NULL
1000
500
5 NULL
8. C. FINDING THE LENGTH OF THE LIST
1000 2000 3000 4000
int length()
{
int count=0; temp
struct node *temp;
temp=root;
while (temp!=NULL)
{
count++;
temp=temp->link;
}
return count;
}
NULL
root
1000 10 NULL
2000 20 NULL
3000 40 NULL
30 NULL
4000
1000
9. D. Inserting a node at specific position after a node
void after-add ()
{
struct node *temp, *p;
int loc, i=1,len;
printf(“Enter Location”);
scanf(“%d”, &loc); 1000 2000 3000 4000
len=length();
if (loc>len)
{
printf(“Invalid location”); root
printf(“Currently list is having a %d nodes”,len); 4500
} p
else
{
p=root;
while(i<loc) temp
{
p=p->link;
i++;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->link=p->link;
p->link=temp;
}
}
10 2000 20 3000 30 4000
40
NULL
35
NULL
1000
1000
4500
10. 2. DELETING A NODE IN THE LIST
A. Deleting a node in middle or end of a node
void delete()
{
struct node *temp;
int loc;
printf(“enter a location to delete”);
scanf(“%d”,&loc);
if(loc>length())
{
printf(“Invalid location”);
} 1000 2000 3000 4000
else
{
if(loc==1)
{
temp=root;
root=temp->link; root
temp->link=NULL; P q
free (temp);
}
else
{
struct node *p=root,*q; temp
int i=1;
while(i<loc-1)
{
p=p->link;
i=i+1;
}
q=p->link;
p->link=q->link;
q->link=NULL;
free(q);
}
}
}
10 2000 10 3000 10 4000
40
NULL
1000
1000 3000
1000
11. 3. SEARCHING A NODE IN THE LIST
Void search()
{
struct node *temp;
temp=root;
if(temp==NULL)
{ 1000 2000 3000 4000
printf(“list is empty”);
}
else
{ root
while(temp!=NULL) temp
{
if (key==temp->info)
break; key
temp=temp->link;
}
if(temp==NULL) Not a key
{
printf(“ search is unsuccessful”);
return;
}
printf(“ search is successful”);
}
}
10 2000 40 NULL
30 4000
20 3000
1000
1000
10
50
12. 4. DISPLAYING ALL ELEMENTS IN THE LIST
1000 2000 3000 4000
void display()
{
struct node *temp;
temp=root;
if(temp==NULL)
{ temp
printf(“list is empty”);
}
else
{
while(temp!=NULL)
{
printf(“%d”, temp->data);
temp=temp->link;
}
}
}
NULL
root
1000 10 NULL
2000 20 NULL
3000 40 NULL
30 NULL
4000
1000
13. DISADVANTAGES OF SINGLY LINKED LIST
Using singly linked list, it is not possible to traverse
the list backwards
i.e two way traversing is not possible
Insertion or deletion to the left of a designated node
X is difficult, this requires the finding the predecessor
of X which takes more time.
Given a Node X , It is difficult to find the
predecessor of X . Since it is required to traverse the
list from the first node
14. DOUBLY LINKED LIST
It is one of the most powerful variations of linked
list.
It is a linear collection of nodes where each node is
divided into three fields.
Using such list , it is possible to traverse the list in
forward and backward direction. Such list is known
as Two way list
*left data *right
15. REPRESENTATION OF DOUBLY LINKED LIST IN C
The nodes in a linked list are self referential
structures.
A structure is a collection of one or more fields which
are of same or different types. self referential
structure is a structure which has at least one field
which is pointer to same structure
Ex:
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
16.
root
Pictorial representation of doubly linked list
Operations performed on doubly linked linked list are :
1.Inserting a node into the list
2.Deleting node from the list
3.Search in a list
4.Display the contents of list
NULL 10
2000
1000 20 3000
3000 40
NULL
2000 30 4000
1000
1000 2000 3000 4000
17. 1.INSERTING A NODE INTO THE LIST
A. Inserting the node at rear end
void append()
{
struct node *temp;
temp=(struct node *)malloc (size of(struct node));
printf(“enter node data);
scanf(“%d”,&temp->data);
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
} 1000 2000 3000 4000
else
{
struct node *p; root
p=root;
while(p->right!=NULL) p
{ temp
p=p->link;
}
p->right=temp;
temp->left=p;
}
NULL 10
2000
1000 20 3000
NULL 40
NULL
2000 30
NULL
1000
1000
4000
22. Singly linked list doubly linked list
1. Since there is only one link , so
traversing is done only in one direction
1. Since there are two links, so traversing
can be done in both directions
2. While deleting a node, its predecessor is
required and can be found only after
traversing from the beginning of list.
2.While deleting a node X,it predecessor
can be obtained using *left of node X,No
need to traverse the list.
3.Occupy less memory 3.Occupy more memory
4.Prgrams will be lengthy and need more
time to design
4.Using circular linked list with header,
efficient and small programs can be written
and hence design is easier
5.Care is taken to modify only one link of a
node
5.Care is taken to modify both links of a
node
SINGLY LINKED LIST VS DOUBLY LINKED LIST
23. ADVANTAGES OF LINKED LIST
Linked list are a dynamic data structure since it
allocating and deallocating the memory while the
programing is running.
Insertion and deletion node operations are easily
implemented in a linked list.
Dynamic data structures such as stacks and queues can
be implemented using linked list.