Answer to Question #228901 in C for Arup

Question #228901

Write a C code to convert a doubly linked list into a circular doubly linked list.


1
Expert's answer
2021-08-23T13:39:52-0400
//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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS