Write a C code to convert a doubly linked list into a circular doubly linked list.
//Converting a double linked list to a circular linked list in C programming language
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int element;
struct Node *nextNode;
struct Node *prevNode;
}list;
void print(list *head){
list *tempNode=head;
printf("The list is as follows :\n%d->",head->element);
head=head->nextNode;
while(head!=tempNode)
{
printf("%d->",head->element);
head=head->nextNode;
}
printf("%d\n",tempNode->element);
return;
}
int main(){
list *node=NULL,*head,*tempNode;
int ch;
do
{
head=(list *)malloc(sizeof(list));
if(head!=NULL)
{
printf("\nInsert an element into the list : ");
scanf("%d",&head->element);
head->nextNode=node;
if(node==NULL)
{
node=head;
head->prevNode=node;
head->nextNode=node;
}
else
{
tempNode=node;
while(tempNode->nextNode!=node)
{
tempNode=tempNode->nextNode;
}
tempNode->nextNode=head;
head->prevNode=tempNode;
}
}
else
{
printf("\nMemory allocation not possible");
}
printf("\nEnter 1 to continue inserting elements into the list or enter 2 to display the list : ");
scanf("%d",&ch);
}while(ch==1);
print(node);
return 0;
}
Comments
Leave a comment