Write c program to perform following functions on Circular Doubly Linked List 1) Create Circular Doubly Linked List 2) Display Circular Doubly Linked List
Circular Doubly Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//To create structure cdll
struct cdll
{
int data;
struct cdll *next, *prev;
};
typedef struct cdll node;
//To define functions
node * getnode();
node * createcdll(node * head);
void displaycdll(node * head);
void main()
{
node * head;
int ch;
do
{
printf("\n1.Create CDLL.\n2.Display CDLL\nOther than above to EXIT.");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch (ch)
{
case 1:
head=createcdll(head);
break;
case 2:
displaycdll(head);
break;
default:
printf("\nYou have chosen to EXIT. Press any key to exit.");
}
}while(ch==1||ch==2);
getch();
}
//To create node
node * getnode()
{
node * temp=NULL;
temp=(node *) malloc(sizeof(node));
if(temp==NULL)
printf("\nMAE");
{
printf("\nEnter the data: ");
scanf("%d",&temp->data);
temp->next=NULL;
temp->prev=NULL;
}
return temp;
}
//To create Linked List
node * createcdll(node * head)
{
node * temp=NULL, *ptr=NULL;
int i,n;
printf("\nEnter the number of nodes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
temp=getnode();
if(head==NULL)
{
head=temp;
head->next=head;
head->prev=head;
}
else
{
ptr=head;
while(ptr->next!=head)
{
ptr=ptr->next;
}
ptr->next=temp;
temp->prev=ptr;
temp->next=head;
head->prev=temp;
}
}
return head;
}
//To display elements in Linked List
void displaycdll(node * head)
{
node *ptr=NULL;
if(head==NULL)
printf("\nCDLL EMPTY.");
else
{
ptr=head;
printf("\nContent is: ");
do
{
printf(" %d",ptr->data);
ptr=ptr->next;
}while(ptr!=head);
}
}
0 Comments