Write programs to perform following operations using functions:
a. Creation of Doubly Circular Linear Linked list
b. Display of Doubly Circular Linear Linked list
c. Insert a node in different positions of Doubly Circular Linear Linked list
d. Delete a node from different positions of Doubly Circular Linear Linked list
#include<stdio.h>
#include<stdlib.h>
typedef struct linkList
{
int data;
struct linkList* next;
struct linkList* prev;
}node;
node* createLinkedList( node*l)
{
char ch;
node* h=l;
while(1)
{
printf("ENTER DATA : ");
scanf("%d",&l->data);
fflush(stdin);
printf("WANT TO CREATE ANOTHER NODE ? (Y/N) : ");
scanf("%c",&ch);
if(ch=='n'||ch=='N')
{
l->next=h;
h->prev=l;
return h ;
}
l->next=(node*)malloc(sizeof(node));
l->next->prev=l;
l=l->next;
}
}
void display(node*l)
{
int ch;
printf("\n first node %d",l->data);
while(1)
{
printf("\n1-> NEXT NODE \n2-> PREVIOUS NODE\n3-> EXIT");
scanf("%d",&ch);
switch(ch)
{
case 1: l=l->next;
printf("next data is : %d",l->data);
break;
case 2: l=l->prev;
printf("previous data is : %d",l->data);
break;
case 3: return;
}
}
}
node* findLastNode(node * l)
{
node* h=l;
while(l->next!=h )
{
l=l->next;
}
return l;
}
node* insertBegining(node *l, int x)
{
node *temp,*last,*h=l;
temp=(node*)malloc(sizeof(node));
last=findLastNode(h);
temp->data=x;
l->prev=temp;
temp->next=l;
temp->prev=last;
l=temp;
last->next=l;
return l;
}
node* findNode(node * l,int f)
{
while(l!=NULL &&l->data!=f)
{
l=l->next;
}
return l;
}
void insertSpecific(node*l,int x,int f)
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=x;
l=findNode(l,f);
if(l==NULL)
printf("ELEMENT NOT FOUND\n");
temp->next=l->next;
temp->prev=l;
l->next=temp;
l->next->prev=temp;
}
void insertLast(node*l,int x)
{
node *temp,*h=l;
temp=(node*)malloc(sizeof(node));
temp->data=x;
l=findLastNode(l);
temp->next=h;
l->next=temp;
temp->prev=l;
temp->next=h;
h->prev=temp;
}
node* insert(node*l)
{
int ch,x,f;
while(1)
{
printf("ENTER YOUR CHOISE : \n 1-> INSER FORM BEGINING \n 2-> INSERT IN A SPECIFIC LOCATION\n 3-> INSERT AT LAST\n 4->DISPLAY\n 5- >EXIT");
scanf("%d",&ch);
if(ch==1 || ch==2 ||ch==3)
{
printf("ENTER THE DATA TO BE INSERT : ");
scanf("%d",&x);
}
switch(ch)
{
case 1: l=insertBegining(l,x);
break;
case 2: printf("ENTER THE ELEMENT AFTER WHICH %d IS TO BE INSERTED :",x);
scanf("%d",&f);
insertSpecific(l,x,f);
break;
case 3: insertLast(l,x);
break;
case 4: display(l);
break;
case 5:return l; ;
}
}
return l;
}
node * deleteFirst(node*l)
{
node* temp =l,*last;
l=l->next;
last=findLastNode(l);
l->prev=last;
last->next=l;
free(temp);
return l;
}
node* findprevNode(node * l,int f)
{
node*h=l;
while(l->next!=h &&l->next->data!=f)
{
l=l->next;
}
return l;
}
void deleteSpecific(node*l,int f)
{
node* temp ;
l=findprevNode(l,f);
if(l->data !=f)
{
printf("data not found");
return;
}
temp=l->next;
l->next=l->next->next;
l->next->next->prev=l;
free(temp);
}
node* findpreLastNode(node*l)
{
node* h=l;
while(l->next->next!=h)
{
l=l->next;
}
return l;
}
void deleteLast(node* l)
{
node* temp,*h=l ;
l=findpreLastNode(l);
temp=l->next;
l->next=h;
h->prev=l;
free(temp);
}
node * deleteNode(node*l)
{
int ch,f;
while(1)
{
printf("ENTER YOUR CHOISE : \n 1-> DELETE THE FAST NODE \n 2-> DELETE A SPECIFIC NODE\n 3-> DELETE THE LAST\n 4->DISPLAY\n 5- >EXIT");
scanf("%d",&ch);
switch(ch)
{
case 1: l=deleteFirst(l);
break;
case 2: printf("ENTER THE ELEMENT WHICH YOU WANT TO DELETE ");
scanf("%d",&f);
deleteSpecific(l,f);
break;
case 3: deleteLast(l);
break;
case 4: display(l);
break;
case 5: return l;
}
}
}
int main()
{
node*h;
int ch;
h=(node*)malloc(sizeof(node));
h=createLinkedList(h);
display(h);
while(1)
{
printf("\nENTER YOU CHOISE\n 1-> INSERT \n 2-> DELETE\n 3-> DISPLAY\n 4-> EXIT ");
scanf("%d",&ch);
switch(ch)
{
case 1: h=insert(h);
break;
case 2: h=deleteNode(h);
break;
case 3: display(h);
break;
case 4:return 0;
}
}
return 0;
}
OUTPUT :
Post a Comment
0 Comments