0% found this document useful (0 votes)
5 views

Circular N Doubly Linked List

about linked lists

Uploaded by

Keerthi Rishitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Circular N Doubly Linked List

about linked lists

Uploaded by

Keerthi Rishitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Circular and Doubly Linked

List

Dr. Meenakshi Choudhary


Assistant Professor
SRM University AP
Introduction
Circular version of singly linked list, where the last node is connected to the first node.

No node will contain the address field as NULL.

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350

The list can be traversed in single direction, yet the from any node, we can reach to any other node in the list.

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.
Creating a circular linked list
struct node *new= (struct node*) malloc(sizeof(struct node));

new->data = value;
new->next = NULL; value NULL 10 NULL

new

Make the head pointer pointing to this node


new
Head = new;
new->next = Head; 1000

Head 10 NULL Head 10 1000

1000
Head = new;
new->next = Head;

3
Add the Next Node to the List

struct node *new= (struct node*) malloc(sizeof(struct node));


2050
new->data = value;
new->next = NULL; new 20 NULL

new
Now the first node will point to new node

Head->next = new;
new->next = Head; Head 10 2050 20 NULL
1000
1000 2050

Head 10 2050 20 1000

4
Add the Third Node to the List

In such case, we need to traverse the list to get the address of last node.

For this, we need a temporary pointer, lets P.

Head 10 2050 20 1000


P = Head;
P
Increment the pointer P to reach to last node

While (P->next != Head)


P = P->next; Head 10 2050 20 1000

P
Insert the new node

P->next = new; 1000


new->next = head; Head 10 2050 20 1000 30 NULL
1000 P New
Create the list in this manner

5
Traversal of a circular linked list
Traverse the list and display the content. Start with the “Head” pointer and go through all the nodes until we
reach last node.

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350

struct node* P = Head; P


while(P->next!=Head)
P = P->next;
printf(“Displaying the list content”);
printf(P->data);
Time complexity = O(n)
Output: Displaying the list content
10 20 30 40

6
Insertion in the Circular Linked List
Inserting a node in the circular linked list such that its basic property is satisfied.

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350 new
80 NULL

A new node can be inserted at any position in the list, however we are considering three distinct cases.
• Insertion at beginning
• Insertion at last
• Insertion after a specific node based on
Key
Index (or position) of node
Insert at first position

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350

Insert P
80 NULL
new

In circular linked list, the insert operation at the beginning requires traversing the entire list, as we need to
change the next field of the last node.

struct node* P = Head;


while(P->next!=Head)
P = P->next;
new
80 NULL

P->next = new; Head


10 3150 20 3250 30 3350 40 3050
3050 3150 3250 3350
new P
80 NULL
3050
new->next = Head;
10 3150 20 3250 30 3350 40 3050
Head
3050 3150 3250 3350

new
Head = new; 80 3050

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350
Analysis of Insertion at beginning operation on the circular linked list

Time complexity = O(n)

Number of pointers to change: 3

• New->next
• P->next
• Head
Insert at last position

10 3150 20 3250 30 3350 40 3050 80 NULL


Head
3050 3150 3250 3350 new
P
P->next = new; Insert

New->next = Head;

10 3150 20 3250 30 3350 40 3050 80 NULL


Head
3050
3050 3150 3250 3350 new

P
Analysis of Insertion at last operation on the circular linked list

Time complexity = O(n)

Number of pointers to change: 2

• New->next
• P->next
Insert at Specific position
Task 1: Insert a new node in the given list after the node containing key value as 30.

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350

P
80 NULL
new->next = P->next
new
P->next = new

Number of pointers to change: 2


Time complexity = O(n) • New->next
• P->next
Assignment Questions
Task 1: Insert a new node in the given circular list after the node containing key value as 30.

Task 2: Insert a new node in the given circular list after the 3rd node.

Task 3: Search a key in the circular list and return the position of node.
Deletion from the Circular Linked List

Delete an element from the list.

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350

Deletion operation can also be performed at various locations in the list such as:

• Delete the first node


• Delete the last node
• Delete the specified node in terms of
Key value
position
Deletion from beginning also requires to traverse the list till last node.

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350
P

P ->next = Head->next;

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350

P = Head;
Head 10 3150 20 3250 30 3350 40 3050
3050 3150 3250 3350
P
Head = Head->next;
10 3150 20 3250 30 3350 40 3050
3050 3150 3250
P
Head 3350

Int x = P->data;
P->next = NULL;
10 NULL 20 3250 30 3350 40 3050
3050 3150 3250 3350
P
Head

Free(P);
Return x;
20 3250 30 3350 40 3050
3150 3250 3350
Head
Analysis of Deletion from last operation on the circular linked list

Time complexity = O(n)

Number of pointers to change: Atleast 2

• P->next
• Head
Task 1: Delete a node from the given list from last.

10 3150 20 3250 30 3350 40 3050


Head
3050 3150 3250 3350
q P
while(p->next != Head)
q = p;
p = p->next;

q->next = Head;
10 3150 20 3250 30 3350 40 3050
Head
3050 3150 3250 3350
q P
10 3150 20 3250 30 3350 40 NULL
P->next = NULL; Head
3050 3150 3250 3350
q P

Int x = P->data;
Free(P);

10 3150 20 3250 30 3350


Head
3050 3150 3250

Number of pointers needed: 2


Time complexity = O(n) Number of pointers to change: Atleast 1
• q->next
Doubly Linked List (DLL)
In doubly linked list, each node contains address to the next and previous nodes, i.e. two address fields are
there.

NULL 10 3150 3050 20 3250 3150 30 3350 3250 40 NULL

3050 3150 3250 3350


Head

In doubly linked list, we can move in both directions. However, it also provides sequential access to the
elements.

We store two address fields in each node of the DLL, it requires more memory than singly linked list.

The insertion and deletion operation in the DLL is complex as they require to change more number of pointers.
Structure of a node of DLL

Struct node
{
Struct node *prev;
prev data next
int data;
Struct node * next;
}
new

struct node *new= (struct node*) malloc(sizeof(struct node));

new->prev = NULL;
new->data = value; NULL 10 NULL
new->next = NULL;

Head = new Head NULL 10 NULL


Add next node to the DLL
1040 2040

Head NULL 10 NULL NULL 20 NULL

new
Head->next = new;
New->prev = Head 1040 2040

Head NULL 10 2040 1040 20 NULL

new

Add another next node to the DLL

1040 2040

Head NULL 10 2040 1040 20 NULL NULL 30 NULL

new
Add this node here
1040 2040 2150

Head NULL 10 2040 1040 20 2150 2040 30 NULL

This time we will need a temporary variable

P->next = new;
New->prev = P;

Number of pointers needed: 1


Time complexity = O(n) Number of pointers to change: 2
• q->next
• New->prev
Insertion in the Doubly Linked List
At first position

NULL 5 NULL
new

NULL 10 3150 3050 20 3250 3150 30 3350 3250 40 NULL

3050 3150 3250 3350


Head

Head->prev = new;
New->next = Head;
Head->new;
NULL 5 3050
new 3020

3020 10 3150 3050 20 3250 3150 30 3350 3250 40 NULL

3050 3150 3250 3350


Head

Head->prev = new;
New->next = Head; Number of pointers needed: 0
Time complexity = O(1) Number of pointers to change: 3
Head->new; • Head->prev
• New->next
• Head
Insert the node somewhere in the middle
new
NULL 5 NULL

NULL 10 3150 3050 20 3250 3150 30 3350 3250 40 NULL

3050 3150 3250 3350


Head
P
Insert

New->next = P->next;

P->next->prev = new; Number of temp pointers needed: 1


Time complexity = O(n)
Number of pointers to change: 4
New->prev = P;
P->next = new;
Deletion in the Doubly Linked List

Delete a node from the DLL, and release the memory occupied by that node.

Delete the First node


NULL NULL
NULL 10 3150 3050 20 3250 3150 30 3350 3250 40 NULL

3050 3150 3250 3350


Head
temp Head

Head 3050 20 3250 3150 30 3350 3250 40 NULL


Head = Head->next;
3150 3250 3350
Head->prev = NULL

NULL 10 NULL
temp->next = NULL temp
3050
Head 3050 20 3250 3150 30 3350 3250 40 NULL
Int x = temp->data;
3150 3250 3350
Free(temp);
Return x;

Number of temp pointers needed: 1


Time complexity = O(1)
Number of pointers to change: At least 2
Assignment Questions
Task 1: Insert a new node in the given doubly list at last.

Task 2: Insert a new node in the given doubly list after the node containing key value as 30.

Task 3: Insert a new node in the given list after the 3rd node.

Task 4: Search a key in the doubly list and return the position of node.

Task 5: Delete a node from the doubly linked list.


i. delete the node containing the given key element.
ii. Delete the node with given node position.
iii. Delete the node after the node containing the given key element.
iii. Delete the last node.

Task 6: Traverse the doubly list in reverse direction and display the elements.

You might also like