C LinkedList
C LinkedList
and Applications)
We have discussed singly and doubly linked lists in the following posts.
Circular linked list is a linked list where all nodes are connected to form a circle. There is no
NULL at the end. A circular linked list can be a singly circular linked list or doubly circular
linked list.
2) Useful for implementation of queue. Unlike this implementation, we dont need to maintain
two pointers for front and rear if we use circular linked list. We can maintain a pointer to the last
inserted node and front can always be obtained as next of last.
3) Circular lists are useful in applications to repeatedly go around the list. For example, when
multiple applications are running on a PC, it is common for the operating system to put the
running applications on a list and then to cycle through them, giving each of them a slice of time
to execute, and then making them wait while the CPU is given to another application. It is
convenient for the operating system to use a circular list so that when it reaches the end of the list
it can cycle around to the front of the list. (Source
http://web.eecs.utk.edu/~bvz/cs140/notes/Dllists/)
4) Circular Doubly Linked Lists are used for implementation of advanced data structures like
Fibonacci Heap.
Why Circular? In a singly linked list, for accessing any node of linked list, we start traversing
from the first node. If we are at any node in the middle of the list, then it is not possible to access
nodes that precede the given node. This problem can be solved by slightly altering the structure
of singly linked list. In a singly linked list, next part (pointer to next node) is NULL, if we utilize
this link to point to the first node then we can reach preceding nodes. Refer this for more
advantages of circular linked lists.
The structure thus formed is circular singly linked list look like this:
In this post, implementation and insertion of a node in a Circular Linked List using singly linked
list are explained.
Implementation
To implement a circular singly linked list, we take an external pointer that points to the last node
of the list. If we have a pointer last pointing to the last node, then last -> next will point to the
first node.
The ponter last points to node Z and last -> next points to node P.
Why have we taken a pointer that points to the last node instead of first node ?
For insertion of node in the beginning we need traverse the whole list. Also, for insertion and the
end, the whole list has to be traversed. If instead of start pointer we take a pointer to the last node
then in both the cases there wont be any need to traverse the whole list. So insertion in the
begging or at the end takes constant time irrespective of the length of the list.
Insertion
A node can be added in three ways:
Insertion in an empty list
After insertion, T is the last node so pointer last points to node T. And Node T is first and last
node, so T is pointing to itself.
Function to insert node in an empty List,
struct Node *addToEmpty(struct Node *last, int data)
{
// This function is only for empty list
if (last != NULL)
return last;
return last;
}
After insertion,
return last;
}
After insertion,
return last;
}
return last;
}
p = p -> next;
} while (p != last -> next);
cout << item << " not present in the list." << endl;
return last;
}
Following is a complete program that uses all of the above methods to create a circular singly
linked list.
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
return last;
}
return last;
}
return last;
}
cout << item << " not present in the list." << endl;
return last;
}
while(p != last->next);
// Driven Program
int main()
{
struct Node *last = NULL;
traverse(last);
return 0;
}