You are on page 1of 11

Data Structures and Algorithms

Circular Linked List

Prepared By : Vaishali Koria Data Structure and Algorithms 1


Circular Linked List
• Linked list is a linear data structure.

• List of elements are logically continuous, but not necessarily physical.

25 2000 67 3000 89 1000


Head= 1000 2000 3000

• Next field of last node will keep the address of first node.

Prepared By : Vaishali Koria Data Structure and Algorithms 2


Circular Linked List
Consider the list to store the roll numbers of three topper students: 25,67,89
Consider this as a node

25 2000 Linked list will be :

25 2000 67 3000 89 1000


Data Address of
next data Head= 1000 2000 3000

Address of
this node is Head pointer will keep the address
suppose of first node of the linked list.
1000

This is called Circular Linked List.

Prepared By : Vaishali Koria Data Structure and Algorithms 3


Circular Linked List operations:
1. Insert at front of the linked list: (Insert_at_front)

Step 1 : Create a new node structure

struct node
Data Pointer to next node
{
int data;
struct node * next;
} *head = NULL;

Step 2 : Allocate Address


struct node* create(int v)
{
struct node* new1;
new1 = (struct node*)malloc(sizeof(struct node));
new1->data = v;
new1-> next = NULL;
return new1; 10 NULL
}
new1= 1000 (assumed)
Consider call : Create(10)

Prepared By : Vaishali Koria Data Structure and Algorithms 4


Circular Linked List operations:
1. Insert at front of the linked list: (Insert_at_front)

Step 3 : Insert a new node in the linked list


Case 1: No node in the linked list

struct node* Insert_at_front(int v)


{
struct node* new1; 5 1000
new1 = create(v);
if (head == NULL)
{ head = new1= 1000
head = new1;
head->next= head;
}
}

Consider call : insert_at_front(5);

Prepared By : Vaishali Koria Data Structure and Algorithms 5


Circular Linked List operations:
1. Insert at front of the linked list: (Insert_at_Front)

Step 3 : Insert a new node in the linked list


Case 2: insert node at front in the existing linked list with nodes Existing linked list:

struct node* Insert_at_front(int v) 5 1000


{
struct node* new1,t; 8 NULL
new1 = create(v); head = 1000
if (head == NULL) new1 = 1500
{
head = new1;
}
else
{
new1next= head; 8 1000
NULL 5 1500
1000
while(t->next!=head) head=1000
1000
head
new1==1500
1500
t =t->next;
head = new1; t= 1000
t->next = head;

}
}

Consider call : Insert_at_front(8);


Prepared By : Vaishali Koria Data Structure and Algorithms 6
Circular Linked List operations:
2. Insert at end of the linked list: (Insert_at_end)
Case 1 will be similar to previous example. 8 1000 10 1500

Case 2: insert node at end in the existing linked list with nodes head = 1500 1000

15 NULL
i. Create new node: new1= 3000

8 1000 10 3000 15 1500


ii. This node should be inserted like
head = 1500 1000 new1=3000
this:

iii. We need to change the next part of node with address 1000.
1000->next = new1(3000)
new1->next = head (1500)

- Last node’s feature is : its next field is head. That we can use to find the address of last node.

Prepared By : Vaishali Koria Data Structure and Algorithms 7


Circular Linked List operations:
2. Insert at end of the linked list: (Insert_at_end)
Case 2: insert node at end in the existing linked list with nodes
iv. Find the address of last node:

8 1000 10 1500 15 NULL


struct node * t = head;
head = 1500 1000 new1= 3000
while(t->next != head)
{ 1. t= 1500 1. t= 1000
t = t-> next;
2. t->next != head 2. t->next == head
}
3. t= t->next So, t= 1000 (address of last node)

iv. Insert node at end


8 1000 10 3000 15 1500
t-> next = new1;
head = 1500 1000 3000
new1-> next = head;

-Will this code work if there is only one node in the list? Find and resolve with code.

Prepared By : Vaishali Koria Data Structure and Algorithms 8


Circular Linked List operations:
3. Delete from front
Case 1: No node in the linked list
if(head== NULL)
{
printf(“No node in the list to delete”);
}
Existing linked list:
Case 2: Delete Front Node 8 1000 10 3000 15 1500
//Find the last node and update its next field. head = 1500 1000 3000

struct node * t = head; Resultant linked list should be:


while(t->next != head)
{ 10 3000 15 1000
t = t-> next; head=1000 3000
}
head = head -> next;
t-> next = head;

-Will this code work if there is only one node in the list? Find and resolve with code.

Prepared By : Vaishali Koria Data Structure and Algorithms 9


Circular Linked List operations:
4. Delete last node (Delete from end)
Case 2: Delete last Node Existing linked list:
- Reach to the last node. 8 1000 10 3000 15 1500

struct node * t = head , *pred; head = 1500 1000 3000

while(t->next != head) t= 1500 t= 1000 t= 3000


{ t->next=1000!=head t->next=3000!=head t->next==head
pred = t; // before going to next node, store the address pred=t=1500 pred=t=1000
of previous node in pred
t =t->next = 3000
t = t-> next; t =t->next = 1000
}
t= 3000 pred= 1000 Resultant linked list should be:
8 1000 10 1500
pred->next = head; head = 1500 1000

-Will this code work if there is only one node in the list? Find and resolve with code.

Prepared By : Vaishali Koria Data Structure and Algorithms 10


Thank You!!!

Prepared By : Vaishali Koria Data Structure and Algorithms 11

You might also like