You are on page 1of 12

Doubly Circular Linked List

Circular Doubly Linked List has properties of both doubly linked list and circular linked list in
which two consecutive elements are linked or connected by previous and next pointer and the last
node points to first node by next pointer and also the first node points to last node by the previous
pointer.
Graphical Representation

Algorithm
Begin:
We shall create a class circulardoublylist which have the following
functions:
nod *create_node(int) = To memory allocated for node dynamically.
insert_begin() = To Insert elements at beginning of the list.
A) If the list is empty, then insert the node and set next and previous
pointer as NULL.
B) If the list is not empty, insert the data and set next and previous
pointer and update them.

insert_end() = To Insert elements at end of the list:


A) If the list is empty create a node as circular doubly list.
B) Find last node.
C) Create node dynamically.
D) Start going to be the next of new node.
E) Make new node as previous node.
F) Make last previous of new node.
G) Make new node next of old last.

insert_pos() = To insert elements at a specified position of the list:


A) Insert the data.
B) Enter the position at which element to be inserted.
C) If the list is empty insert node at first.
D) If list is not empty find node having position and next node.
E) Insert the node between them.

delete_pos() = To delete elements from specified position of the list:


A) If list is empty, then return.
B) Enter the position from which node needs to be deleted.
C) If list has one node delete it and update next and prev pointers.
D) If list has more than one nodes, then delete the node at particular
position and update next and prev pointer.

search() = To search element in the list:


A) If the list is empty, then return.
B) Enter the value to be searched.
C) Print the position at which element to be found.
D) If the element is not found, print not found.

update() = To update value at a particular node:


A) If the list is empty, then return.
B) Enter the position of node to be updated.
C) Enter the new value.
D) Update the node.
display() = To display the list.
reverse() = To reverse the list.
End

Insertion in Circular Doubly Linked List:

• Insertion at the end of list or in an empty list


• Empty List (start = NULL): A node(Say N) is inserted with data = 5, so previous
pointer of N points to N and next pointer of N also points to N. But now start pointer
points to the first node the list.

• List initially contains some nodes, start points to first node of the List: A node(Say M) is
inserted with data = 7, so previous pointer of M points to last node, next pointer of M points
to first node and last node’s next pointer points to this M node and first node’s previous
pointer points to this M node.

Python Code to insert value at the beginning of the double linked list:

# Function to insert at the end


def insertEnd(value) :
global start

# If the list is empty, create a


# single node circular and doubly list
if (start == None) :

new_node = Node(0)
new_node.data = value
new_node.next = new_node.prev = new_node
start = new_node
return
# Find last node */
last = (start).prev

# Create Node dynamically


new_node = Node(0)
new_node.data = value

# Start is going to be next of new_node


new_node.next = start

# Make new node previous of start


(start).prev = new_node

# Make last previous of new node


new_node.prev = last

# Make new node next of old last


last.next = new_node

Insertion at the beginning of the list:


To insert a node at the beginning of the list, create a node(Say T) with data = 5, T next pointer
points to first node of the list, T previous pointer points to last node the list, last node’s next pointer
points to this T node, first node’s previous pointer also points this T node and at last don’t forget to
shift ‘Start’ pointer to this T node.

Insertion at the beginning: Code


# Function to insert Node at the beginning of the List,
def insertBegin( value) :
global start
# Pointer points to last Node
last = (start).prev
new_node = Node(0)
new_node.data = value # Inserting the data
# setting up previous and
# next of new node
new_node.next = start
new_node.prev = last
# Update next and previous pointers
# of start and last.
last.next = (start).prev = new_node
# Update start pointer
start = new_node

Insertion in between the nodes of the list: To insert a node in between the list, two data values are
required one after which new node will be inserted and another is the data of the new node.

Insertion at specific position: Code


# The new node is inserted after the node with value2
def insertAfter(value1, value2) :
global start
new_node = Node(0)
new_node.data = value1 # Inserting the data
# Find node having value2 and
# next node of it
temp = start
while (temp.data != value2) :
temp = temp.next
next = temp.next
# insert new_node between temp and next.
temp.next = new_node
new_node.prev = temp
new_node.next = next
next.prev = new_node

Delete Operations:
Algorithm:
The List initially contains some nodes, start points at the first node of the List,
1. If the list is not empty, then we define two pointers curr and prev_1 and initialize the
pointer curr points to the first node of the list and prev_1 = NULL.
2. Traverse the list using the curr pointer to find the node to be deleted and before moving
from curr to the next node, every time set prev_1 = curr.
3. If the node is found, check if it is the only node in the list. If yes, set start = NULL and free
the node pointing by curr.
4. If the list has more than one node, check if it is the first node of the list. The condition to
check this is (curr == start). If yes, then move prev_1 to the last node(prev_1 = start ->
prev). After prev_1 reaches the last node, set start = start -> next and prev_1 -> next = start
and start -> prev = prev_1. Free the node pointing by curr.
5. If curr is not the first node, we check if it is the last node in the list. The condition to check
this is (curr -> next == start). If yes, set prev_1 -> next = start and start -> prev = prev_1.
Free the node pointing by curr.
6. If the node to be deleted is neither the first node nor the last node, declare one more pointer
temp and initialize the pointer temp points to the next of curr pointer (temp = curr->next).
Now set, prev_1 -> next = temp and temp ->prev = prev_1. Free the node pointing by curr.
• If the given key(Say 4) matches with the first node of the list(Step 4):
• If the given key(Say 8) matches with the last node of the list(Step 5):

• If the given key(Say 6) matches with the middle node of the list(Step 6):

Implementation in Python:
def deleteNode(start, key):
# If list is empty
if (start == None):
return None
# Find the required node
# Declare two pointers and initialize them
curr = start
prev_1 = None
while (curr.data != key) :
# If node is not present in the list
if (curr.next == start) :
print ("List doesn't have node", “with value = ", key)
return start
prev_1 = curr
curr = curr.next
# Check if node is the only node in list
if (curr.next == start and prev_1 == None) :
(start) = None
return start
# If list has more than one node,
# check if it is the first node
if (curr == start) :
# Move prev_1 to last node
prev_1 = (start).prev
# Move start ahead
start = (start).next
# Adjust the pointers of prev_1 and start node
prev_1.next = start
(start).prev = prev_1
# check if it is the last node
elif (curr.next == start) :
# Adjust the pointers of prev_1
# and start node
prev_1.next = start
(start).prev = prev_1
else :
# create new pointer,
# points to next of curr node
temp = curr.next
# Adjust the pointers of prev_1
# and temp node
prev_1.next = temp
temp.prev = prev_1
return start
Complete Code:

# Structure of a Node
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

# Function to insert at the end


def insertEnd(value) :
global start
# If the list is empty, create a
# single node circular and doubly list
if (start == None) :
new_node = Node(0)
new_node.data = value
new_node.next = new_node.prev = new_node
start = new_node
return
# If list is not empty
# Find last node */
last = (start).prev
# Create Node dynamically
new_node = Node(0)
new_node.data = value
# Start is going to be next of new_node
new_node.next = start
# Make new node previous of start
(start).prev = new_node
# Make last previous of new node
new_node.prev = last
# Make new node next of old last
last.next = new_node
# Function to insert Node at the beginning
# of the List,
def insertBegin( value) :
global start
# Pointer points to last Node
last = (start).prev
new_node = Node(0)
new_node.data = value # Inserting the data
# setting up previous and
# next of new node
new_node.next = start
new_node.prev = last
# Update next and previous pointers
# of start and last.
last.next = (start).prev = new_node
# Update start pointer
start = new_node
# Function to insert node with value as value1.
# The new node is inserted after the node with
# with value2
def insertAfter(value1, value2) :
global start
new_node = Node(0)
new_node.data = value1 # Inserting the data
# Find node having value2 and
# next node of it
temp = start
while (temp.data != value2) :
temp = temp.next
next = temp.next
# insert new_node between temp and next.
temp.next = new_node
new_node.prev = temp
new_node.next = next
next.prev = new_node

# function to delete node


def deleteNode(start, key):
# If list is empty
if (start == None):
return None
# Find the required node
# Declare two pointers and initialize them
curr = start
prev_1 = None
while (curr.data != key) :
# If node is not present in the list
if (curr.next == start) :
print ("List doesn't have node", “with value = ", key)
return start
prev_1 = curr
curr = curr.next
# Check if node is the only node in list
if (curr.next == start and prev_1 == None) :
(start) = None
return start
# If list has more than one node,
# check if it is the first node
if (curr == start) :
# Move prev_1 to last node
prev_1 = (start).prev
# Move start ahead
start = (start).next
# Adjust the pointers of prev_1 and start node
prev_1.next = start
(start).prev = prev_1
# check if it is the last node
elif (curr.next == start) :
# Adjust the pointers of prev_1
# and start node
prev_1.next = start
(start).prev = prev_1
else :
# create new pointer,
# points to next of curr node
temp = curr.next
# Adjust the pointers of prev_1
# and temp node
prev_1.next = temp
temp.prev = prev_1
return start
# function to display
def display() :
global start
temp = start
print ("Traversal in forward direction:")
while (temp.next != start) :
print (temp.data, end = " ")
temp = temp.next
print (temp.data)
print ("Traversal in reverse direction:")
last = start.prev
temp = last
while (temp.prev != last) :
print (temp.data, end = " ")
temp = temp.prev
print (temp.data)

# Driver Code
if __name__ == '__main__':
global start
# Start with the empty list
start = None
# Insert 5. So linked list becomes 5.None
insertEnd(5)
# Insert 4 at the beginning. So linked list becomes 4.5
insertBegin(4)
# Insert 7 at the end. So linked list becomes 4.5.7
insertEnd(7)
# Insert 8 at the end. So linked list becomes 4.5.7.8
insertEnd(8)
# Insert 6, after 5. So linked list becomes 4.5.6.7.8
insertAfter(6, 5)
print ("Created circular doubly linked list is: ")
display()

You might also like