- Write a C program to convert a singly linked list to a circular linked list.
- How to make a circular linked list from singly linked list.
Given a singly linked list, we have to convert it to a circular linked list. We have to set the next pointer of tail node to point back to head node to complete a cycle.
Algorithm to convert singly linked list to Circular linked list
Let "head" be the head pointer of given linked list. Singly linked list is a linear data structure, which can be traversed from first node(head node) till last node(tail node) in one direction. Tail node of linked list points to NULL, Hence it is a dead end. We cannot traverse beyond tail node. To convert a singly linked list to circular linked list, we will set next pointer of tail node to head pointer.
Let "head" be the head pointer of given linked list. Singly linked list is a linear data structure, which can be traversed from first node(head node) till last node(tail node) in one direction. Tail node of linked list points to NULL, Hence it is a dead end. We cannot traverse beyond tail node. To convert a singly linked list to circular linked list, we will set next pointer of tail node to head pointer.
- Create a copy of head pointer, let's say "temp".
- Using a loop, traverse linked list till tail node(last node) using temp pointer.
- Now set the next pointer of tail node to head node. (temp->next = head;)
In this program, we will use a user defined function "convertToCircularLL" which takes head node of singly linked list as input and converts it to a Circular linked list by implementing above algorithm.
void convertToCircularLL(struct node *head){ /* Input Validation */ if(head == NULL){ printf("Error : Invalid Input !!!!\n"); return INT_MIN; } struct node *temp = head; while(temp->next != NULL){ temp = temp->next; } temp->next = head; }
C program to convert a singly linked list to circular linked list
#include <stdio.h> #include <stdlib.h> /* A structure of linked list node */ struct node { int data; struct node *next; } *head; void initialize(){ head = NULL; } /* Given a Inserts a node in front of a singly linked list. */ void insert(int num) { /* Create a new Linked List node */ struct node* newNode = (struct node*) malloc(sizeof(struct node)); newNode->data = num; /* Next pointer of new node will point to head node of linked list */ newNode->next = head; /* make new node as new head of linked list */ head = newNode; printf("Inserted Element : %d\n", num); } void convertToCircularLL(struct node *head){ /* Input Validation */ if(head == NULL){ printf("Error : Invalid Input !!!!\n"); return INT_MIN; } struct node *temp = head; while(temp->next != NULL){ temp = temp->next; } temp->next = head; } /* Prints a linked list from head node till tail node */ void printCircularLinkedList(struct node *head) { struct node *temp = head; do { printf("%d ", temp->data); temp = temp->next; } while(temp != head); } int main() { initialize(); /* Creating a linked List*/ insert(1); insert(2); insert(3); insert(4); insert(5); convertToCircularLL(head); /* Printing Circular Linked List */ printCircularLinkedList(head); return 0; }Output
Inserted Element : 1 Inserted Element : 2 Inserted Element : 3 Inserted Element : 4 Inserted Element : 5 5 4 3 2 1