Upload
jackharish
View
218
Download
0
Embed Size (px)
Citation preview
8/14/2019 C-Programming-Class 12
1/28
1
Session 12
Linked List
8/14/2019 C-Programming-Class 12
2/28
2
Session Objectives
To study the concepts of linked list.
To study the different operations performed
on the linked list.
8/14/2019 C-Programming-Class 12
3/28
3
Session Topics
Introduction to linked list.
Singly linked list.
Circular linked list.
8/14/2019 C-Programming-Class 12
4/28
4
Linked Lists
A linked listis a data structure which is a
collection of zero or more nodes where each
nodehas some information. Between, each node
in the list, there exists a logical relationship so
that given the address of the first node, any node
in that list can be obtained. Each node can hold
the data along with a pointer field using whichaddress of the next node can be obtained.
8/14/2019 C-Programming-Class 12
5/28
5
Types of Linked Lists:
Singly linked lists
Circularly singly linked listsDoubly linked lists
Circular doubly linked lists
8/14/2019 C-Programming-Class 12
6/28
6
Storage representation of a node
In a linked allocation technique, a node in a linked list has
two fields.
infowhich contains the actual information
linkwhich contains address of the next node
A node can be represented as astructure as follows
struct node
{
int info;struct node *link;
};
8/14/2019 C-Programming-Class 12
7/28
7
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.
10205
info link
nodefirst = 1004
101215
info link
1020
25
info link
1012
Memory representation of singly linked list
5 15 25
info link info infolink link first
node
10041020 1012
Pictorial Representation of Singly linked list
8/14/2019 C-Programming-Class 12
8/28
8
Operations performed on singly linked list
Inserting a node into the list Deleting a node from the list
Display the contents of the list
temp20 30 40
10
first
To insert an item at the front end
10 20 30 40
firstAfter inserting 10
8/14/2019 C-Programming-Class 12
9/28
9
Function to insert an item at the front end of
the list
NODE insert_front(int item,NODE first)
{
NODE temp;temp = get_node(); /* Obtain a node */
temp->info = item; /* Insert the item */
temp->link = first;
return temp; /* Return the first node */}
8/14/2019 C-Programming-Class 12
10/28
10
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;
}
8/14/2019 C-Programming-Class 12
11/28
11
To delete a node from the front end
10 20 30 40temp
first
first
10 20 30 40
temp
20 30 40first
8/14/2019 C-Programming-Class 12
12/28
12
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 */
}
8/14/2019 C-Programming-Class 12
13/28
13
To insert an item at the rear end
2010 30
first
cur
2010 30 40
tempfirst cur
2010 30 40
8/14/2019 C-Programming-Class 12
14/28
14
Function to insert an item at the rear end of
the listNODE 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;
}
8/14/2019 C-Programming-Class 12
15/28
15
Delete a node from the rear end
10first After deleting
first = NULL
10 20 30 40first
prev cur
prev
10 20 30 40first
8/14/2019 C-Programming-Class 12
16/28
16
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;
}
8/14/2019 C-Programming-Class 12
17/28
17
contd.Function to delete an item from the
rear end of the list
/* Obtain address of the last node and just previous to that */
prev = NULL;
cur = first;
while(cur->link!=NULL){
prev = cur;
cur = cur->link;
}
printf(The item deleted is %d\n,cur->info);
freenode(cur); /* Delete the last node */
prev->link = NULL; /* Node pointed to byprevis madethe last node*/
return first; /* Return address of the first node */
}
8/14/2019 C-Programming-Class 12
18/28
18
Circular singly linked list
Linear linked list containing the address of the first node in the linkfield of the last node results in a Circular Singly linked listor
Circular list. last
Pictorial representation of Circular singly linked list
NOTE:
In a circular list if the address of any node x is known, one can traverse theentire list from that node and thus, all nodes are reachable.
Operations performed on a Circular list:
insert _front , insert_rear, delete_front, delete_rear
8/14/2019 C-Programming-Class 12
19/28
19
To insert a node at the front end of a Circular
list
20 30 40
last
20 30 40
10
last
8/14/2019 C-Programming-Class 12
20/28
20
Function to insert an item at the front end of
the Circular listNODE 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 tempas 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 */
}
8/14/2019 C-Programming-Class 12
21/28
21
Insert a node at the rear end
10 20 30
last
10 20 30 40
last temp
8/14/2019 C-Programming-Class 12
22/28
22
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 */
}
8/14/2019 C-Programming-Class 12
23/28
23
Delete a node from the front end
10 20 30 40first last
20 30 40
To delete an item from the front end
After deleting the first item
8/14/2019 C-Programming-Class 12
24/28
24
Function to delete an item from the front end
of the list
NODE delete_front(NODE last){NODE temp,first;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;
}first=last->link; /*Obtain the item to be deleted*/
/*Store new first node in link of last*/last->link = first->link;printf(The item deleted is %d\n,first->info);free_node(first); /*Delete the old first node */return last;
}
8/14/2019 C-Programming-Class 12
25/28
25
Function to delete a node from the rear end
10 20 30 40
10 20 30
To delete a node from the rear end
After deleting a node from the rear end
8/14/2019 C-Programming-Class 12
26/28
26
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 */
}
8/14/2019 C-Programming-Class 12
27/28
27
Summary
A singly linked list is a linked list, where each node hasdesignated field called link field which contains address ofthe next node.
If there exists only one link field in each and every node Inthe list, then the list is aSingly Linked List.
Linear linked list containing the address of the first node inthe link field of the last node results in a Circular Singlylinked listorCircular list.
In a circular list if the address of any node x is known, onecan traverse the entire list from that node and thus, allnodes are reachable.
8/14/2019 C-Programming-Class 12
28/28
28
Thank You!