Circular Singly Linked
List
06/04/2025 Dept. of I&CT 1
Disadvantages of Singly Linked List
• There is only one link field and hence traversing is done in only one
direction
• To delete a designated node X, address of the first node in the list
should be given
06/04/2025 Dept. of I&CT 2
Circular Singly Linked List
• A circular list is a variation of the linear linked list in which last
node is connected to the first node. The address field of the last node
contains address of the first node.
06/04/2025 Dept. of I&CT 3
Advantages of Circular List
• Every node is accessible from a given node by traversing successively using
the link field
• To delete a node, the address of the first node is not necessary. Search for the
predecessor of the current node(node to be deleted) and proceed with the delete
operation
06/04/2025 Dept. of I&CT 4
Approaches
• A pointer first is designated to point to the starting node of the list. Traverse the list till
the last element (which is the predecessor of the designated first)
• A pointer variable last is designated to point the last node and the node that follows last, will
be the first node of the list.
06/04/2025 Dept. of I&CT 5
//Inserting at beginning using the last ptr //Inserting at end using the last ptr
struct node * insfrl(struct node * last) struct node * inslast(struct node * last)
{ {
struct node * temp=(struct node *)malloc(sizeof(struct struct struct node * temp=(struct node *)malloc(sizeof(struct
node * )); struct node * ));
printf("\nEnter the element:\n“); printf("\nEnter the element:\n“);
scanf(“%d”, &temp->data); scanf(“%d”, &temp->data);
if(last==NULL)
if(last==NULL)
{
{
last=temp;
last=temp;
temp->link=last;
temp->link=last;
}
}
else
else {
{ temp->link=last->link;
temp->link=last->link; last->link=temp;
last->link=temp; last=temp;
} }
return last; return last;
}06/04/2025 Dept. of I&CT } 6
//Inserting at end using the first ptr
//Inserting at beginning using the first ptr void print(struct node * head)
struct node * insrt(struct node * head) struct node * insfrnt(struct node * head) {
{ { struct node * h=head;
struct node * temp=(struct node struct node * temp=(struct node printf(“%d”,h->data);
*)malloc(sizeof(struct node));
*)malloc(sizeof(struct node)); h=h->link;
struct node * cur;
struct node * cur; while(h!=head)
printf("Enter the value to be inserted:“); printf("Enter the value to be inserted:“); {
scanf(“%d”, &temp->data); scanf(“%d”, &temp->data); printf(%d”, h->data);
temp->link=NULL; temp->link=NULL; h=h->link;
if(head==NULL) { if(head==NULL) { }
head=temp; head=temp; }
temp->link=head; temp->link=head;
} } struct node * printl(struct node *
else { else { last)/*print using last pointer*/
cur=head; temp->link=head; {
while(cur->link!=head) cur=head; struct node * h=last->link;
cur=cur->link; while(cur->link!=head) while(h!=last)
cur->link=temp; cur=cur->link; {
temp->link=head; cur->link=temp; printf(“%d”, h->data);
}
head=temp; h=h->link;
return head;
} }
return head; printf(“%d”, h->data);
}
} }
06/04/2025 Dept. of I&CT 7
//Deleting an element from the end using first
pointer
struct node * delfe(struct node * head) cur=head;
{
while(cur->link->link!=head)
struct node * cur;
{
if(head==NULL)
cur=cur->link;
{
printf("No records to delete“);
}
return NULL; struct node * t=cur->link;
} cur->link=head;
if(head->link==head) printf(“Item deleted:%d“, t->data);
{ free(t);
printf("Deleted item:%d”, head->data); return head;
free(head); }
return NULL;
}
06/04/2025 Dept. of I&CT 8
//Deleting an element from the end using a last
pointer struct node * cur=last->link;
struct node * delle(struct node * last) while(cur->link!=last)
{ {
if(last==NULL) cur=cur->link;
{ }
printf(“No elements to delete:“);
cur->link=last->link;
return NULL;
printf(“Item deleted:%d “,last->data);
}
if(last->link==last)
free(last);
{ last=cur;
printf("Element deleted is:%d“, last->data); return last;
free(last); }
return NULL;
06/04/2025 Dept. of I&CT 9
}
//Deleting an element from the beginning using
last pointer free(last->link);
struct node * dellb(struct node * last) return NULL;
{ }
struct node * cur; cur=last->link;
if(last==NULL) last->link=cur->link;
{ printf(“Item deleted:%d”, cur->data);
printf(“No nodes to delete“); fee(cur);
return NULL; return last;
} }
if(last->link==last)
{
printf("Element
06/04/2025 deleted is:%d”, last->data);
Dept. of I&CT
10