Linked List
Linked List
Chapter 2
Linked List
Linked list is a linear collection of data elements called nodes. Each node is divided into 2
fields. The first part is for info or data field and second part for pointer or link field to the
next node. The general representation is
INFO LINK
Where INFO is used to store the data value, LINK is used to store the address of next node.
Example:
START
5 8 3 NULL
Here START is an external pointer it stores the address of the first node. NULL indicates it
is the end of linked list.
Advantages of Linked List
Efficient memory utilization
Insertion and Deletion operation are easier
Dynamic Data Structure
Extensive manipulation
Arbitrary memory location
Efficient memory utilization
Memory can be allocated when ever we are inserting a new node and we deallocate when it is
deleted.
Insertion and Deletion operation are easier
Linked List flexibility in inserting a data item at a specified position and Deletion of a data
item from a given position.
Dynamic Data Structure
Linked List can grow or shrink during the execution of program.
Extensive manipulation
We perform a complex manipulation a Linked Link without having any prior knowledge of
memory space available.
Arbitrary memory location
In Linked List memory location are not consecutive, it may arbitrary. But the data is linked
with previous node. So, we can store the data in affecting memory effectively by using the
link value in Linked List.
Disadvantages of Linked List
1. It requires more memory than array because each node has a pointer, which requires
more memory.
UNIT 2 Data Structures Using C 2
struct node
{
int info;
struct node *link;
};
It is a self-referential structure in C. Here link is the pointer that points the next data of same
structure.
Types of Linked List
Linked List are classified into 5 types:
Singly Linked List
Circular Linked List
Doubly Linked List
Header Linked List
Circular Doubly Linked List
Singly Linked List
A singly linked list is a linear collection of data elements called nodes, where each node is
divided into 2 fields: Info field and link field. The link field points the next node and the link
field of last node points NULL.
Example:
START
1 58 24 NULL
0
INFO LINK
The second step get the data item and place in INFO field
START
50
INFO LINK
The third step place NULL value in Link fields
START
50 NULL
INFO LINK
The fourth step will return to main program.
Traversal
The process of visiting all nodes in Linked List is known as traversing. The following
algorithm traverse the Linked List
Algorithm
1. Is list is empty?
If(START==NULL)
Display “Linked list is empty”
Return
End if
2. Assign start value to CURPTR
CURPTR=start
3. Repeat while (CURPTR != NULL)
Print CURPTRINFO
CURPTR=CURPTRLINK
4. Return
Example
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 201 198
5
10 58 24 NUL
L
UNIT 2 Data Structures Using C 5
Insertion
This operation is used to insert a new node in the Linked List. The new node can be inserted
in 3 ways:
1) Insert at the beginning of a Linked List
2) Insert at the end of a Linked List
3) Insert at the specified position in Linked List
1) Insert at the beginning of a Linked List
The following algorithm insert a node at the beginning.
Algorithm:
1. Create a node using malloc
NEWNODE=(NODE*) malloc(sizeof (NODE))
2. Store the data
NEWNODEINFO=ITEM
3. Assign the start value to the link of NEWNODE
NEWNODELINK=start
4. Assign NEWNODE as START
Start=NEWNODE
UNIT 2 Data Structures Using C 6
5. Exit
Explanation
Consider the following linked list contain 3 nodes
START
2010 201 215 1003
0
10 90 24 NUL
L
2025
NEWNODE=2025
The second step stores data in INFO field
2025
50
The third step assign NEWNODE link to START
2025
50 2010
START
2010 201 215 1003
0
10 90 24 NUL
L
The following code illustrates inserting a node in the beginning of the Linked List
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
UNIT 2 Data Structures Using C 7
The first step check START is NULL, if it is NULL it call Insert at beginning otherwise
it assign Start value to CURPTR
The third step is traverse to point the last node
START
UNIT 2 Data Structures Using C 8
2025
NEWNODE=2025
The fifth step stores data in INFO field
2025
50
The sixth step assign NEWNODE link to CURPTR LINK and NEWNODE
LINK = NULL. The pictorial representation is
START
2010 2010 215 100 2025
3
10 90 24 50 NULL
The following code illustrates inserting a node at the end of the Linked List
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the data value for the node:" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
UNIT 2 Data Structures Using C 9
2025
NEWNODE=2025
In fifth step it assigns INFO Value to NEWNODE
2025
50
UNIT 2 Data Structures Using C 10
In sixth step it creates link between CURPTR and NEWNODE. Consider pos is 2.The
pictorial representation is as follows.
START
2010 2010 2025 215 1003
10 50 90 24 NULL
The following code illustrates inserting a node at a specific position of the Linked List
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the position for the new node to be inserted:");
scanf("%d",&pos);
printf("\nEnter the data value of the node:");
scanf("%d",&temp->info) ;
temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}
DELETION
This operation is used to delete a node from the Linked List. A node can be deleted using
three ways:
UNIT 2 Data Structures Using C 11
The first step check Start is NULL. If it is print list is empty otherwise step2
In second step assign CURPTR by Start (Here Start = 2002 therefore, CURPTR=
2002)
In third step set the CURPTR LINK to Start
In fourth step free the CURPTR
The pictorial representation after deletion is
START
2015 201 198
5
58 24 NULL
The following code illustrates delete a node from the beginning of the Linked List
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty\n");
return;
UNIT 2 Data Structures Using C 12
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is : %d",ptr->info);
free(ptr);
}
}
The first step check Start is NULL. If it is print list is empty otherwise step2
In second step it check is list contain one element if it is set Start is NULL otherwise
step4
In third step delete start
In fourth step Assign start to CURPTR
In fifth step set PREPTR to NULL
In sixth step traverse the list until CURPTR points the last node.
In seventh step delete CURPTR
In eight step set PREPTR LINK as NULL
START
2002 2002 2015
10 58 NULL
The following code illustrates delete a node from end of the Linked List
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty\n");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
}
Algorithm
1. if (POS = = 1)
call delete_beginning()
else
goto step 2
2. set CURPTR to Start
CURPTR=Start
3. set PREVPTR to NULL
PREVPTR = NULL
4. set CURPTR to specified position using loop
for(i=1; i<POS; i++)
{
PREVPTR=CURPTR
CURPTR=CURPTR LINK
}
5. create link between PREVPTR and the nextnode of CURPTR
PREVPTR LINK = CURPTR LINK
free(CURPTR)
6. Exit
Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 201 198
5
10 58 24 NUL
L
The first step check POS= = 1 if it call delete_beginning otherwise goto step2
In second step Assign start to CURPTR
In third step set PREPTR to NULL
In fourth step CURPTR as specified position using loop.
In fifth step set link between PREPTR and next node of CURPTR. Then delete the
CURPTR.
The following code illustrates delete a node from a specific position of the Linked List
void delete_pos()
{
UNIT 2 Data Structures Using C 15
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is: %d",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d",ptr->info );
free(ptr);
}
}
}
Length
This operation is used to find the number of nodes in Linked List
Algorithm
1. Initialize len=0
2. If (Start = = NULL)
{
Print “List is Empty”
Return
}
UNIT 2 Data Structures Using C 16
3. Set CURPTR=Start
4. while(CURPTR != NULL)
{
len++
CURPTR= CURPTR LINK
}
5. return len
Explanation
Consider a Linked List with the following nodes:
START
2002 2002 201 198
5
10 58 24 NUL
L
In the first step it initializes the variable len as 0
In the second step it checks whether the Start is NULL , Here Start =2002
In the third step it assigns the Start value to CURPTR that is CURPTR =2002
In the fourth step it checks CURPTR is NULL if it is not then it increment Len value by 1
and set CURPTR= CURPTR LINK. Repeat until CURPTR is NULL
In fifth step it returns len value , Here len=3
Searching
It is a process of searching an element in a linked list.
Algorithm
1. Assign CURPTR= start, LOC = NULL
2. Repeat step 3 while (CURPTR != NULL)
3. If(ITEM = = CURPTR INFO)
{
LOC= CURPTR
Print “Search Successful”
}
Else
CURPTR= CURPTR LINK
4. If (LOC = = NULL)
Print “Search Unsuccessful”
5. Exit
5
10 58 24 NUL
L
In circular linked list, only one pointer is used for insertion and deletion.
UNIT 2 Data Structures Using C 18
This node does not contain actual data but contains useful information about the entire
linked list such as total number of nodes, pointer to the last node etc.
The start points to the header node in the header linked list.
Link field of header node points the first node in the list.
The header linked list will always contain a header node even if list is empty.
Header linked list are of two types:
Grounded header linked list
Circular header linked list
Grounded header linked list
The grounded header linked list is also referred as singly header linked list. In this the last
node LINK field contains the NULL pointer.
Example
START
2002 2002 201 198
5
58 24 NUL
L
Header node