Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by previous and next pointer and the last node points to first node by next pointer and also the first node points to last node by previous pointer.
#include<stdio.h> typedef struct mynode { int info; struct mynode *lptr; struct mynode *rptr; }Node; Node *start=NULL,*last=NULL; void display_shulto() { Node *tptr=start->rptr; printf("\nDATA shulto:\n"); printf("->%d",start->info); while(tptr!=start) { printf("->%d",tptr->info); tptr=tptr->rptr; } } void display_ulto() { Node *tptr=last; printf("\nDATA ulto:\n"); while(tptr!=start) { printf("->%d",tptr->info); tptr=tptr->lptr; } printf("->%d",start->info); } //Insertion at the End function void add_end(int value) { Node *newp=NULL,*tptr=NULL; newp=(Node*)malloc(sizeof(Node)); newp->info=value; newp->lptr=NULL; newp->rptr=NULL; if(start==NULL) { start=newp; } else { tptr=start; while(tptr->rptr!=start) { tptr=tptr->rptr; } tptr->rptr=newp; newp->lptr=tptr; last=newp; } newp->rptr=start; start->lptr=newp; } //Main int main() { char ch; int value,i,pos; printf("Enter Y to continue or N to exit:"); scanf("%c",&ch); //Insertion while(ch!='N') { printf("Enter the info:"); scanf("%d",&value); fflush(stdin); //End add_end(value); printf("Node Inserted at the end\n"); printf("Enter Y to continue or N to exit:"); scanf("%c",&ch); fflush(stdin); } display_shulto(); display_ulto(); return 0; }
Comments