Doubly and Circular
Linked List
Doubly Linked List
• A Doubly Linked List (DLL) contains an extra pointer, typically
called previous pointer, together with next pointer and data which are
there in singly linked list.
Advantages over singly linked list
1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to
be deleted is given.
3) We can quickly insert a new node before a given node.
In singly linked list, to delete a node, pointer to the previous node is
needed. To get this previous node, sometimes the list is traversed.
In DLL, we can get the previous node using previous pointer.
Disadvantages over singly linked list
1) Every node of DLL Require extra space for an previous pointer
2) All operations require an extra pointer previous to be maintained.
For example, in insertion, we need to modify previous pointers
together with next pointers.
Basic Operations
Following are the basic operations supported by a DLL
• Creation of DLL
• Insertion Beginning − Adds an element at the beginning of the list
• Deletion Beginning − Deletes an element at the beginning of the list.
• Insert Last − Adds an element at the end of the list.
• Delete Last − Deletes an element from the end of the list.
• Insert After − Adds an element after an item of the list.
• Delete − Deletes an element from the list using the key.
• Display forward − Displays the complete list in a forward manner.
• Display backward − Displays the complete list in a backward manner.
InsertAtBegDll(info,prev,next,start,end)
1.create a new node and address in assigned to ptr.
2.check[overflow] if(ptr=NULL)
write:overflow and exit
3.set Info[ptr]=item;
4.if(start=NULL)
set prev[ptr] = next[ptr] = NULL
set start = end = ptr
else
set prev[ptr] = NULL
next[ptr] = start
set prev[start] = ptr
set start = ptr
[end if]
5.Exit.
Insertion At Last in doubly linked list
1.create a new node and address in assigned to ptr.
2.check[overflow] if(ptr=NULL)
write:overflow and exit
3.set Info[ptr]=item;
4.if(start=NULL)
set prev[ptr] = next[ptr] = NULL
set start = end = ptr
else
set prev[ptr] = end
next[end] = ptr
set ptr[next] = NULL
set end = ptr
[end if]
5.Exit.
InsertAtlocDll(info,prev,next,start,end,loc,size)
repeat steps a and b while(n != nloc)
1.set nloc = loc-1 , n=1
a. loc = next[loc]
2.create a new node and address in b. n = n+1
assigned to ptr. [end while]
3.check[overflow] if(ptr=NULL) next[ptr] = next[loc]
write:overflow and exit prev[ptr] = loc
prev[next[loc]] = ptr
4.set Info[ptr]=item; next[loc] = ptr
5.if(start=NULL) else
set prev[ptr] = next[ptr] = NULL set prev[ptr] = end
set start = end = ptr next[end] = ptr
set ptr[next] = NULL
else if(nloc<=size) set end = ptr
[end if]
6.Exit.
Deletion in DLL code
void deleteNode(struct DLL **head, struct DLL *ptr)
{
if(*head == NULL || ptr == NULL)
return;
//if the node is head
if(*head == ptr)
*head = ptr->next;
//change the next and prev only if they are not null
if(ptr->next != NULL)
ptr->next->prev = ptr->prev;
if(ptr->prev != NULL)
ptr->prev->next = ptr->next;
free(ptr); //delete the node
}
Backward Traversal
1. Start
2. If (START is equal to NULL)
3. Display “The list is Empty”
Stop
4. Initialize TEMP = TAIL
Repeat the step 5 and 6 until (TEMP == NULL )
5. Display “TEMP → DATA”
6. TEMP = TEMP → Prev
7. Stop
Searching in DLL
Write a function that searches a given key ‘x’ in a given singly linked list. The
function should return true if x is present in linked list and false otherwise.
Iterative solution:
bool search(Node *head, int x)
Initialize a node pointer, current = head.
Do following while current is not NULL
a) current->key is equal to the key being searched return true.
b) current = current->next
Return false
Recursive Solution
bool search(head, x)
1)If head is NULL, return false.
2)2) If head's key is same as x, return true;
3) 2) Else return search(head->next, x)
Circular Linked list(CLL)
• 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.
Applications of CLL
1) Any node can be a starting point. We can traverse the whole list by
starting from any point. We just need to stop when the first visited node is
visited again.
2) Useful for implementation of queue. We don’t need to maintain two
pointers for front and rear if we use circular linked list.
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.
4) Circular Doubly Linked Lists are used for implementation of advanced data
structures like Fibonacci Heap.
Practice..
• A circularly linked list is used to represent a Queue. A single variable p
is used to access the Queue. To which node should p point such that
both the operations enQueue and deQueue can be performed in
constant time?
Rear Node