Doubly Circular Linked List

As we learned in a previous tutorial the basic of doubly linked list.

The doubly linked list means we create a node with a 3 field which stores data and two address pointer as a previous and next node address.

Doubly circular state that our end node will point to start node and start node will point to end node to form a circular linked list.

Doubly Circular Linked List

As its circular doubly linked list initially if we have one node then that node point to itself.

Doubly Circular Linked List 2
Doubly Circular Linked List 1

Each node stores its previous and next node address, As its circular doubly linked list last node point to first and first node point to last node.

Program

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222#include<stdio.h>#include<alloc.h>struct node{    int data;    struct node *next;    struct node *prev;}struct node *start;void insert_beg(void);void insert_end(void);void insert_before(void);void display(void);void deleteion(void);void destroy(void);#define pf printf#de4fine sc scanfvoid main(void){int ch;start=NULL;do{pf(“\n 1. insert_beg\n 2. insert_end\n 3. insert_before\n 4. Display\n 5. Deletion\n 6.Destroy\n7.Exit”);sf(“%d”,&ch);switch(ch){case 1:   insert_beg();  break;case 2:insert_end();break;case 3:insert_before();break;case 4:display();break;case 5:deletion();break;case 6:destroy();break;case 7:exit(0);break;}}while(ch!=7);}void insert_beg(void){struct node *nn,*lp;nn=(strcut node *)malloc(sizeof(struct node));pf(“Enter data”);sf(“%d”,&nn->data);if(start==NULL){nn->next=nn;nn->prev=nn;start=nn;}else{lp=start->prev;nn->prev=lp;nn->next=start;start->prev=nn;lp->next=nn;start=nn;}}void insert_end(void){struct node *nn ,*lp;nn=(strcut node *)malloc(sizeof(struct node));pf(“Enter data”);sf(“%d”,&nn->data);if(start==NULL){nn->prev=nn;nn->next=nn;start=nn;}else{lp=start->prev;nn->next=start;nn->prev=lp;start->prev=nn;lp->next=nn;}}void insert_before(void){struct node *nn ,*temp,*ptemp;int x;if(start==NULL){pf(“DLL is empty”);return;}pf(“Enter data which you want to insert the node”);sf(“%d”,&x);if(x==start->data){insert_beg();            return;}temp=start;while(temp!=NULL && temp->data!=x){ptemp=temp;temp=temp->next;if(temp==start){pf(“%d does not exist:”,x);return;}}nn=(strcut node *)malloc(sizeof(struct node));pf(“Enter data”);sf(“%d”,&nn->data);nn->prev=ptemp;nn->next=temp;ptemp->next=nn;temp->prev=nn;}void display(void){struct node *temp*ptemp;if(start==NULL){pf(“\n DLL is empty”);return;}pf(“Forward order”);temp=start;do{pf(“%d”,temp->data);temp=temp->next;}while(temp!=start);temp=start->prev;pf(“reverse order”);do{pf(“%d”,temp->data);temp=temp->prev;}while(temp!=start->prev);}void deletion(void)   {struct node *ptemp,*temp,*ntemp,*lp;int x;if(start==NULL){pf(“\n DLL is empty”);return;}pf(“Enter  data to delete”);sf(“%d”,&x);if(x==start->data){if(start==start->next){temp=start;start=null;free(temp);}else{temp=start;lp=start->prev;start=start->next;free(temp);start->prev=lp;lp->next=start;}return;}temp=start;while(temp ->data!=x){ptemp=temp;temp=temp->next;if(temp==start){pf(“%d does not exist”);return;}}ptemp->next=temp->next;ntemp=temp->next;ntemp->prev=ptemp;free(temp);}}void destroy(void)   {struct node *temp,*dp;if(start==NULL){pf(“\n DLL is empty”);return;}temp=start;while(temp!=NULL){dp=temp;temp=temp->next;free(dp);}start=NULL;}