Module 12 – part 2 – Circular and
Doubly Linked Lists
BITS Pilani Dr. Ashutosh Bhatia and Dr. Asish Bera
Pilani Campus
Department of Computer Science & Information Systems
Module Overview
• Circular Linked Lists
• Doubly Linked Lists
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Circular Liked Lists and cycles in a Linked
List
Circular Linked List
head
10 next 8 next 4 next 20 next
• The next of the last node points to the first node
• Result:
• Traversing the list is an infinite operation as you will never
encounter a NULL pointer
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
More examples
head
10 next 8 next 4 next
head
10 next 8 next 4 next 20 next
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Detecting cycles in linked list
Cycle in a linked list
Solution 1:
• Traverse the list. While traversing, store the addresses of all the
visited nodes in an array/table.
• When we traverse from node1 to node2, check if the address
of node2 already exists in the table. If YES, a cycle is detected.
If not, add address of node 2 to the table and go forward.
• If you encounter a NULL in the traversal, then the list doesn’t
have a cycle.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Cycle in a linked list
Solution 2: This solution requires modifications to the basic linked list
data structure.
• Have a visited flag with each node
• Traverse the linked list and keep marking visited nodes
• If you see a visited node again then there is a cycle
• If you encounter a NULL in the traversal, then the list doesn’t have a
cycle.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Cycle in a linked list
Solution 3: This is the fastest method
• Traverse linked list using two pointers (slow_p & fast_p)
• Move (slow_p) by one node and fast_p by two.
• If these pointers meet at the same node then there is a cycle.
• If pointers do not meet then linked list doesn’t have a cycle. OR if
either pointer encounters a NULL in the traversal, then the list
doesn’t have a cycle.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Solution 3 illustrated
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Doubly Liked Lists
Doubly Linked Lists
head
prev ele next prev ele next prev ele next NULL
NULL
• Each node now stores:
• value of the element stored at that node: ele
• address of the next node: next
• address of the previous node: prev Supports two-
• The head node contains the address of the first node way traversal of
• The next of the last node points to NULL, meaning end of the list linked lists
• The prev of the first node points to NULL, meaning beginning of the list
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Doubly Liked Lists – Implementation
Structure definitions for doubly
linked lists
head
prev ele next prev ele next prev ele next NULL
1st node 2nd node 3rd node
NULL
Consider that our doubly linked list stores integer elements.
struct dllnode{ struct doubly_linked_list{
int ele; int count;
struct dllnode * next; struct dllnode * head;
struct dllnode * prev; };
};
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Creating a new doubly linked
list
typedef struct dllnode * DLLNODE; typdef struct doubly_linked_list * DLIST;
struct dllnode{
struct doubly_linked_list{
int ele;
DLLNODE next; int count;
DLLNODE prev; DLLNODE head;
};
};
DLIST createNewList(){
DLIST myList;
myList = (DLIST) malloc(sizeof(struct doubly_linked_list));
// myList = (DLIST) malloc(sizeof(*myList));
myList->count=0;
myList->head=NULL; myList count=0
return myList;
head=NULL
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Creating a new node
typedef struct dllnode * DLLNODE; typdef struct doubly_linked_list * DLIST;
struct dllnode{
struct doubly_linked_list{
int ele;
DLLNODE next; int count;
DLLNODE prev; DLLNODE head;
};
};
DLLNODE createNewNode(int value){
DLLNODE myNode;
myNode = (DLLNODE) malloc(sizeof(struct dllnode));
// myList = (DLLNODE) malloc(sizeof(*myNode));
myNode->ele=value; prev=NULL
myNode->next=NULL; myNode ele=value
myNode->prev=NULL;
return myNode; next=NULL
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Inserting a node into the list
void insertNodeIntoList(DLLNODE n1, DLIST l1){
prev NULL
// case when list is empty
if(l1->count == 0) { count=0 10
l1->head = n1; head NULL next NULL
n1->next = NULL;
n1->prev = NULL;
l1->count++;
}
// case when list is non empty
else { count=1 prev NULL
... ... head
10
}
} next NULL
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Inserting a node into the list
(contd.)
void insertNodeIntoList(DLLNODE n1, DLIST l1){
// case when list is empty
if(l1->count == 0) {
... ...
}
// case when list is non empty
else {
n1->next = l1->head;
n1->prev = NULL; NULL
n1->next->prev = n1;
l1->head = n1;
l1->count++; prev
NULL
}
} n1 8
NULL
next prev prev prev
count=3 4 10 30 25
l1 head
next next next NULL
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Removing a node from the
beginning of the list
void removeFirstNode(LIST l1)
{
if (l1->count == 0)
{
printf("List is empty. Nothing to remove\n");
Do we have to
} bother about
else removing this
{ link?
NODE temp = l1->head;
l1->head = temp->next; NULL
l1->head->prev = NULL;
free(temp);
l1->count--; prev NULL
}
return;
8
} prev prev prev
next
count=4 3 10 30 25
l1 head NULL
next next next
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Other functions (exercise)
Exercise: Implement the following functions for a linked list:
• insertNodeAtEnd(DLIST mylist, DLLNODE n1) : inserts n1 at the end
of mylist
• insertAfter(DLIST mylist, DLLNODE n1, int v): inserts n1 into
mylist after a node containing a value v
• removeLastNode(DLIST mylist) : removes the last node from mylist
• search(int data, DLIST mylist): returns the node that contains its
ele=data
• printList_forward(DLIST mylist): prints the elements present in the
entire list in a sequential fashion in forward direction starting from the first element
• printList_backward(DLIST mylist): prints the elements present in the
entire list in a sequential fashion in backward direction starting from the last element
• removeElement(int data, DLIST mylist): removes the node that has
its ele=data
• isEmpty(DLIST mylist): checks if the list is empty or not
• Modify the insert/delete functions to first check whether the list is empty using
isEmpty() function.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus