Class 04 Circular Linked Lists
Class 04 Circular Linked Lists
Harbi Al-Mahafzah
5/8/2018 1
Circular Linked List
The difference between a circular and singly
linked list that in a singly list the last node is
pointed to Null while the last node in circular list
is pointed to the head.
head last
5/8/2018 2
# include<iostream.h> C++ code to create Circular linked list
struct nod {
int Data ;
nod *link ;
};
nod *head, *cur, *last ;
void create_Cir ( )
{
head = new nod ;
cur = head ;
for (int i = 1 ; i <= 4 ; i++ )
{
cur -> link = new nod ;
cur = cur -> link ;
}
last = cur ;
last -> link = head ;
}
main( )
{ create_Cir ( ) ; }
5/8/2018 3
Reading D ata
void read_Cir( )
{
cout<< "Enter the value for the head : " ;
cin>> head -> Data ;
cur = head ;
while( cur->link !=head)
{
cout<< " Enter the values of the items : " ;
cur = cur -> link ;
cin>> cur -> Data ;
}
}
5/8/2018 4
Suppose we have insert the following
data 1,2,5,6 then the list becomes
head last
1 2 5 6
5/8/2018 5
Traversing a single linked list
To traverse a circular single list we mean to visit every node
in the list starting from the head node to the last node. The
structure of the function is as follows:
1. void write( )
2. {
3. int l = 0;
4. cur = head ;
5. cout<< "Data of node [ " <<l++<<" ] = "
6. <<head ->Data<<endl;
7. while (cur -> link != head)
8. {
9. cur = cur -> link;
10. cout<< "Data of node [ " <<l++<<" ] = "
11. <<cur->Data<<endl;
12. }
13. }
5/8/2018 6
Basic Operations on Circular Lists
I nser tion
for inser ting a new item into the list
D eletion
for deleting an item from an existing linked list.
5/8/2018 7
Insert Item: “At the beginning”
To insert a new node at the beginning of the Circular linked
list we can use the following function:
1. void adfirst_Cir(int m) {
2. if (!head) {
3. head = new nod;
4. head->Data = m;
5. last = head;
6. last -> link = head;
7. }
8. else {
9. nod *temp = new nod;
10. temp -> Data = m;
11. temp -> link = head;
12. head = temp;
13. last -> link = head;
14. }
15. }
5/8/2018 8
nod *temp = new nod;
temp -> Data = m;
head last
1 2 5 6
tem p
m
*tem p
5/8/2018 9
temp -> link =head;
head = temp;
last
1 2 5 6
head
5/8/2018 10
If the value of m=0 the list will become
like this
head last
0 1 2 5 6
5/8/2018 11
Insert Item: “At the End”
1. void adend_Cir(int n)
2. {
3. if (!=last)
4. {
5. last = new nod;
6. last -> Data = n;
7. head = last;
8. last -> link = head; }
9. else
10. {
11. nod *temp = new nod;
12. temp -> Data = n;
13. last -> link = temp;
14. last = temp;
15. last -> link = head ; }
16.
5/8/2018
} 12
else part
nod *temp = new nod;
temp -> Data = n;
head last
1 2 5 6
tem p
n
5/8/2018 *tem p 13
last-> link = temp;
last = temp;
last -> link = head;
head
1 2 5 6
last
5/8/2018 14
If the value of n=8 then the will become
head last
1 2 5 6 8
5/8/2018 15
Insert Item: “At any place”
1. void adany_Cir (int me)
2. {
3. nod *temp, *ptr
4. temp = new nod;
5. temp -> Data = me;
6. cur = head;
7. while (cur ->Data < me)
8. {
9. ptr = cur;
10. cur = cur -> link;
11. }
12. ptr -> link = temp ;
13. temp -> link = cur ;
14. }
5/8/2018 16
1. nod *temp, *ptr
2. temp = new nod;
3. temp -> Data = me;
4. cur = head;
head last
1 2 5 6
tem p
me
*ptr
5/8/2018 *tem p 17
while(cur ->Data < me)
{
ptr = cur;
cur = cur -> link;
}
1 2 5 6
tem p
me
5/8/2018 18
If the value of me = 3 the becomes
head last
1 2 3 5 6
5/8/2018 19
Delete Item: “At the beginning”
1. void defirst_Cir( )
2. {
3. cur = head;
4. head = head -> link;
5. delete cur;
6. last -> link = head;
7. }
1 2 5 6
5/8/2018 20
delete cur;
last->link = head;
head last
2 5 6
5/8/2018 21
Delete Item: “At the last”
1. void delast_Cir( )
2. {
3. cur = head;
4. while (cur -> link != last )
5. cur = cur -> link;
6. delete last;
7. last = cur;
8. last -> link = head;
9. }
5/8/2018 22
1. while (cur -> link != last )
2. cur = cur -> link;
3. delete last;
4. last = cur;
5. last -> link = head;
1 2 5 6
head last
1 2 5
5/8/2018 23
Delete Item: “The specified place”
1. void delany_Cir( int me)
2. {
3. assert(head -> Data != me);
4. assert(last -> Data != me);
5. node *ptr;
6. cur = head;
7. while (cur -> Data != me)
8. {
9. ptr = cur;
10. cur = cur -> link;
11. }
12. ptr -> link = cur -> link;
13. delete cur;
14. }
5/8/2018 24
As we do not want to delete either the
head or last nodes we have used the
assert function which is available in
assert.h header file. This function is
extension of if statement as it terminate
the program when the condition does
not satisfied.
assert(head -> Data != me);
assert(last -> Data != me);
5/8/2018 25
If we insert the value 1 which is the value of the head
node or 6 which the value of last node. The following
message will raise:
5/8/2018 26
To reach the specified node we use while as follows:
1. while (cur -> Data != me)
2. {
3. ptr = cur;
4. cur = cur -> link;
5. }
1 2 5 6
5/8/2018 27
The deletion start at the time ptr pointed to
the node which is next to cur
ptr -> link = cur -> link ;
delete cur;
1 2 5 6
head last
1 2 6
5/8/2018 28
Destroy the list
We can destroy the circular linked list by deleting
node after node starting from the head node up to
last node.
1. void destroy_Cir( )
2. {
3. if(! head ) exit(1);
4. cur = head ;
5. while(cur -> link != head)
6. {
7. head = cur -> link;
8. delete cur;
9. cur = head;
10. }
11. cout<<" No linked List exist \n";
12. }
5/8/2018 29
To initialize a circular linked listwhich will
be com pletely em pty(N U LL)
void initila_Cir()
{
head = last = NULL ;
}
5/8/2018 30
Write a complete C++ program to
do the following :
Initialize the Circular Linked List
Add new node at the beginning
Add new node at the end
Add new node place
Delete first node
Delete last node
Delete any node
Destroy the list
5/8/2018 31