C-Programming-Class 12
C-Programming-Class 12
Linked List
1
Session Objectives
2
Session Topics
3
Linked Lists
4
Types of Linked Lists:
5
Storage representation of a node
6
Singly linked list
A singly linked list is a linked list, where each node has designated field
called link field which contains address of the next node. If there exists
only one link field in each and every node In the list, then the list is a
Singly Linked List.
info link info link info link
5 1020 15 1012 25
node
first = 1004 1020 1012
7
Operations performed on singly linked list
• Inserting a node into the list
• Deleting a node from the list
• Display the contents of the list
10
temp
20 30 40
first
10 20 30 40
first
After inserting 10
8
Function to insert an item at the front end of
the list
9
Function to obtain a new node from the
availability list
NODE get_node()
{
NODE x;
/* Try to allocate the required size of memory */
x = (NODE)malloc(sizeof(struct node));
if(x == NULL)
{
printf(“Out of memory\n”);/*Allocation failed*/
exit(0);
}
/* Allocation successful, return address */
return x;
}
10
To delete a node from the front end
first
10 20 30 40
temp
first
10 20 30 40
temp
first 20 30 40
11
Function to delete an item from the front end
of the list
NODE delete_front(NODE first)
{
NODE temp;
if(first == NULL) /* Is list empty */
{
printf(“List empty\n”);
return first;
}
/* Retain the address of the node to be deleted*/
temp = first;
first = first->link; /* Update first */
/* Display and delete the front node */
printf(“The item deleted is %d\n”,temp->info);
free_node(temp);
return first; /* return address of first node */
}
12
To insert an item at the rear end
cur
10 20 30
first
10 20 30 40
10 20 30 40
13
Function to insert an item at the rear end of
the list
NODE insert_rear(int item, NODE first)
{
NODE temp; /* Node to be inserted */
NODE cur; /* To hold the address of the last node*/
temp = get_node();
temp ->info = item;
temp->link = NULL;
/* Return the new node created if list is empty*/
if(first==NULL) return temp;
/* If list exists,obtain address of last node */
cur = first;
while(cur->link != NULL)
{
cur = cur->link;
}
/* Insert the node at the end */
cur->link = temp;
/* Return address of the first node */
return first;
}
14
Delete a node from the rear end
prev cur
first
10 20 30 40
prev
first
10 20 30 40
15
Function to delete an item from the rear end
of the list
NODE delete_rear(NODE first)
{
NODE cur,prev;
if(first == NULL)
{
printf(“List empty!Cannot delete\n”);
return first;
}
if(first ->link == NULL) { /* Only one node is
present and delete it*/
printf(“The item to be deleted is %d\n”,first->info);
freenode(first); /*Return to availability list */
first = NULL;
return first;
}
16
contd….Function to delete an item from the
rear end of the list
NOTE:
In a circular list if the address of any node x is known, one can traverse the
entire list from that node and thus, all nodes are reachable.
Operations performed on a Circular list:
insert _front , insert_rear, delete_front, delete_rear
18
To insert a node at the front end of a Circular
list
last
20 30 40
10
last
20 30 40
19
Function to insert an item at the front end of
the Circular list
NODE insert_front(int item, NODE last)
{
NODE temp;
temp=get_node();/*Create a new node to be inserted*/
temp->info = item;
if(last == NULL)/* Make temp as the first node */
last = temp;
else /*Insert at the front end */
temp->link = last->link;
last->link = temp; /*Link last node to first node*/
return last; /* Return the last node */
}
20
Insert a node at the rear end
last
10 20 30
last temp
10 20 30 40
21
Function to insert an item at the rear end of
the list
NODE insert_rear(int item,NODE last)
{
NODE temp;
temp = get_node; /*Create a new node */
temp->info = item;
if(last == NULL)/*Make temp as the first node*/
last = temp;
else /*Insert at the rear end*/
temp->link = last->link;
last->link = temp; /*Link last node to first node*/
return temp;/*Make the new node as the last node */
}
22
Delete a node from the front end
first last
10 20 30 40
20 30 40
24
Function to delete a node from the rear end
10 20 30 40
10 20 30
25
Function to delete a node from the rear end
NODE delete_rear(NODE last){
NODE prev;
if(last == NULL) /* If no list exists */
{
printf(“List is empty\n”);
return NULL;
}
if(last->link ==last) /* Is there only one node? */
{
printf(“The item deleted is %d\n”,last->info);
free_node(last);
return NULL;
}
prev = last->link; /*Obtain address of previous node*/
while(prev->link != last)
prev = prev->link;
prev->link = last->link;/*prev node is made the last node*/
printf(“The item deleted is %d\n”,last->info);
free_node(last);/*Delete the old node*/
return prev; /* return the new last node */
}
26
Summary
• A singly linked list is a linked list, where each node has
designated field called link field which contains address of
the next node.
• If there exists only one link field in each and every node In
the list, then the list is a Singly Linked List.
• Linear linked list containing the address of the first node in
the link field of the last node results in a Circular Singly
linked list or Circular list.
• In a circular list if the address of any node x is known, one
can traverse the entire list from that node and thus, all
nodes are reachable.
27
Thank You!
28