Circular & Doubly
Linked List
Circular Linked List
CIRCULAR LINKED LIST
In a circular linked list, the last node contains a pointer to the first
node of the list. While traversing a circular linked list, we can begin
and traverse the list in forward direction until we reach the same
node where we started.
Inserting a New Node in a Circular Linked List
Case 1: The new node is inserted at the beginning of the
circular linked list.
Case 2: The new node is inserted at the end of the circular
linked list.
Case 3: insertion In the middle
Insertion at the Beginning
Steps to insert a Node at beginning :
1. The first Node is the Head for any Linked List.
2. When a new Linked List is instantiated, it just has the Head, which is
Null.
3. Else, the Head holds the pointer to the first Node of the List.
4. When we want to add any Node at the front, we must make the head
point to newly added node. And the Next pointer of the newly
added Node, must point to the previous Head, whether it be
NULL(in case of new List) or the pointer to the first Node of the List.
5. The previous Node pointed by Head is now the second Node of
Linked List, because the new Node is added at the front.
class Node class circular_llist
{ {
int info; public:
Node *next; Node *last;
}; circular_llist()
{last = NULL;}
};
Algorithm
void circular_llist::add_begin(int val)
{
node *temp = new Node();
temp->info=val;
if (last == NULL)
{
last=temp;
temp->next=last;
}
else
{
temp->next = last;
last = temp;
}
Algorithm- Comparison
Circular
void circular_llist::add_begin(int val) Single
{
node *temp = new Node(); void insert_beg(int val)
temp->info=val; { node *temp=new node;
if (last == NULL) temp->info=val;
{ If(head==NULL)
last=temp; {
temp->next=last; head=temp;
} temp->next=NULL
else }
{ else{
temp->next = last; temp->next=head;
last = temp; head=temp; }
} }
Insertion at the Middle of Circular Linked List
Insert between Pre and Cur
of a Circular Linked List
New->next = Cur;
Prev->next = New;
10 20 55 70
40
Prev Cur Last
New
Insertion at the Last of Circular Linked List
While (cur->next != Head)
Cur=cur ->next;
cur->next = New;
New->next = Head;
void insertNode( int item,int pos){
Node *New = new Node();
Node *Prev;
Node *Cur;
New->data = item;
if(last == NULL){ // insert into empty
list
last = New;
last->next = last;
}
Prev = last;
Cur = last->next;
for (int i=1;i<pos;i++)
{Prev = Cur;
Cur = Cur->next;
}
New->next = Cur;
Prev->next = New;
}
void insertNode( int item,int pos){
Node *New = new Node();
Node *Prev; void insert_position(int pos, int val)
Node *Cur; {
New->data = item; node *temp=new node;
if(last == NULL)
node *pre;
{
last = New; node *cur;
last->next = last; temp->data=val;
}
If(head==NULL)
Prev = last;
Cur = last->next; {
for (int i=1;i<pos;i++) head=temp;
{ Prev = Cur; temp->next=NULL
Cur = Cur->next;
} }
New->next = Cur; cur=head;
Prev->next = New; for(int i=1;i<pos;i++)
}
{ pre=cur; cur=cur->next; }
temp->next=cur;
pre->next=temp
}
Deletion
Circular Linked List
Delete a node
from a single-node Circular Linked List
Last = NULL;
delete Cur;
10
Last = Cur = Prev
Delete the head node
from a Circular Linked List
while(Prev->next!=last)
{ Prev=Cur; Cur=Cur->next;}
Prev->next = Cur->next;
delete Cur;
10 20 40 55 70
Rear Prev
Cur
Delete a middle node Cur
from a Circular Linked List
For (i=1;i<=position;i++)
{ Prev=Cur; Cur=Cur->next;}
Prev->next = Cur->next;
delete Cur;
10 20 40 55 70
Prev Cur Rear
Delete the end node
from a Circular Linked List
while(Cur->next!=last)
{ Prev=Cur; Cur=Cur->next;}
Prev->next = Cur->next;
delete Cur;
10 20 40 55 70
Prev Cur
Doubly Linked List
Doubly Linked List
A doubly linked list or a two-way linked list is a more
complex type of linked list which contains a pointer to
the next as well as the previous node in the sequence.
Therefore, it consists of three parts—
1. data,
2. a pointer to the next node,
3. a pointer to the previous node
class Node The PREV field of the first node and the
{ NEXT field of the last node will contain
NULL.
int info;
Node *next; The PREV field is used to store the
Node *pre; address of the preceding node, which
enables us to traverse the list in the
};
backward direction.
Inserting a New Node in a
Doubly Linked List
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given
node.
Inserting a Node at the Beginning
node *temp=new node;
temp->info=val; temp->prev=NULL;
If(start==NULL)
{ start=temp; temp->next=NULL;}
else{
temp->next=start;
start->prev=temp;
start=temp;
}
Inserting a Node at the End
node *temp=new node;
temp->info=val; temp->next=NULL;
Node *ptr = start;
while(ptr->next!=NULL)
{ ptr=ptr->next; }
ptr->next=temp;
temp->prev=ptr
Inserting a Node After a Given Node
node *temp=new node; temp->data=data;
cur=start;
while (pre->data!= sp_val)
{ pre=cur; cur=cur->next; }
temp->next=cur; temp->prev=pre;
pre->next=temp;
cur->prev==temp;
Inserting a Node Before a Given Node
in a Doubly Linked List
Deleting a Node
from a Doubly Linked List
Deleting a Node
from a Doubly Linked List
• Case 1: The first node is deleted.
• Case 2: The last node is deleted.
• Case 3: The node after a given node is deleted.
• Case 4: The node before a given node is deleted
Deleting the First Node
if (start==NULL)
cout<<“Underflow<<endl;
else
node *ptr;
ptr = head;
head=headnext;
head->prev=NULL
delete ptr;
Deleting the Last Node
while(Cur->next!=NULL)
{ Prev=Cur; Cur=Cur->next;}
Prev->next = NULL;
delete Cur;
Deleting the Node After a Given Node
cur=start;
while (pre->data!= sp_val)
{ pre=cur; cur=cur->next; }
pre->next=cur->next;
(cur->next)->prev=pre;
delete Cur;
Deleting the Node Before a Given Node
in a Doubly Linked List
Practice Activity
Implement Doubly Linked list by writing
functions for all possible operations in doubly
linked list.
Complexity Analysis of Linked List Operations
List Type Time Complexity
Worst
Search Insertion Deletion
Singly-Linked
List
Doubly-Linked
List
Circular Linked
List