Answer to Question #233184 in C for SOM

Question #233184

WAP to create a single circular double linked list of n nodes and display the linked list by

using suitable user defined functions for create and display operations.


1
Expert's answer
2021-09-05T00:33:07-0400
#include <stdio.h>
#include <stdlib.h>




struct Node 
{
	int data; 
	struct Node *nextNode;
	struct Node *prevNode;
};


void displayList(struct Node *tempNode){
	
	struct Node *tempNode1=tempNode; 
	printf("The circular double linked list :\n%d->",tempNode->data);
	tempNode=tempNode->nextNode;
	while(tempNode!=tempNode1) 
	{
		printf("%d->",tempNode->data);
		tempNode=tempNode->nextNode;
	}
	printf("%d\n",tempNode1->data);
	return;
}


int main(){
	struct Node *headNode=NULL,*tempNode,*tempNode1;
	int c;
	
	
	do
	{
		tempNode=(struct Node *)malloc(sizeof(struct Node));
		if(tempNode!=NULL)
		{
			printf("\nEnter the first element of the linked list: ");
			scanf("%d",&tempNode->data);
			tempNode->nextNode=headNode;
			if(headNode==NULL)
			{	
				headNode=tempNode;
				tempNode->prevNode=headNode;
				tempNode->nextNode=headNode;
			}
			else
			{
				tempNode1=headNode;
				while(tempNode1->nextNode!=headNode)
				{
					tempNode1=tempNode1->nextNode;
				}
				tempNode1->nextNode=tempNode;
				tempNode->prevNode=tempNode1;
			}
		}
		else
		{
		printf("\nMemory not allocated");
		}
		printf("\nEnter 1 to add more elements : ");
		scanf("%d",&c);
	}while(c==1);
	
	displayList(headNode);
	
	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