Unit 3 - LL
Unit 3 - LL
Unit 3 - LL
6. The pointer field of the last node is assigned a NULL value to indicate
end of the list. A pointer to the starting node of the list is used to
access the list. It is called the head pointer, that contains the address
of first node of the list. Once it is available the next successive nodes
can be accessed. Head pointer is also called as the external pointer.
10 20 30 40 50 NULL
head
7. When there is no node in the list it is called empty list. In this case
head pointer contains NULL value.
head NULL
Ex.:
10 20 30 NULL
head
2. Circular linked list
In a circular linked list, the last node is made to point to the first
node. So, there is no NULL pointer.
10 20 30
head
struct node
{
int data;
struct node *next;
};
The above structure has two members, a variable data, to hold the
element and the another element is the variable next, which stores the
address of the next node in the list.
Ex.:
NULL 10 20 30 NULL
head
The above statement declares three pointers, head, points to the starting
node of the list; add, points to every new node to be added to the list and
temp, stores the address of the last node added to the list.
Here, head = NULL, denotes the list is empty.
2. To create the linked list, the dynamic memory allocation function,
malloc is used. This function returns a pointer of type void. Hence it has
to be type cast as Node pointer.
if (head == NULL)
{
head = add;
temp = add;
}
add
20 NULL
if(head!=NULL)
{
temp->next = add;
temp = add;
}
add temp
10 20 NULL
head
This step is repeated for all the remaining nodes.
Insertion
1. For the insert operation a new node needs to be created using malloc
function.
10 20 30 NULL
head
add
40 NULL
4. The new node add, can be inserted at three different positions,
a. At the beginning
b. At the end
The new node, add can be added in the beginning of list as follows,
add->next = head;
head = add;
10 20 30 NULL
head add
40
Here, we need to traverse till the end of the list using a temp pointer
as follows,
temp = head;
while(temp->next!=NULL) temp = temp->next;
temp->next = add;
temp
10 20 30
head
40 NULL
add
In this case, loc=3 and the so, temp pointer needs to placed at node 2.
temp=head;
for(i=1;i<loc-1;i++) temp=temp->next;
add->next = temp->next;
temp->next = add;
temp
10 20 30 NULL
head
add
40
Deletion
For deletion operation item to be deleted is compared with
elements in the list and when node is found its memory is released using
the free function.
Consider the following list,
10 20 30 40 NULL
head
There are two possible cases, either item to be deleted is present in head
node or it is present in any other node.
temp = head;
head = head->next;
free(temp);
20 30 40 NULL
head
10 20 30 40 NULL
head
if(temp==NULL)
printf(“Item not in list”);
else
prev->next = temp->next;
free(temp);
Searching
The process of searching a key element in a linked list requires
traversing through the list comparing each node’s data with key value. The
process ends when either the key value is found, or end of list occurs.
Consider the following list,
10 20 30 40 NULL
head
Suppose, item 30 is to be searched. The variable loc is initially set to 1 and
can be gradually incremented to give location of the key element in the
list. The list can be traversed as follows,
for(temp=head, loc=1 ; temp != NULL; temp=temp->next, loc++)
if(temp->data == item) break;
if(temp==NULL)
printf(“Item not in list”);
else
printf(“Item found at location %d”, loc);
10 20 30 40 NULL
head
2. Here, loc =2
temp
10 20 30 40 NULL
head
3. Here, loc = 3
temp
10 20 30 40 NULL
head
10 20 30 40 NULL
head
10 20 30
head
arr[0] 10
arr[1] 20
arr[2] 30
10 20 30 NULL
arr[3]
arr[4] head
arr