/*Program Name: Doubly Circular Linked List with different operations*/
- #include<iostream.h>
- #include<process.h>
- #include<conio.h>
- struct cll
- {
- int data;
- struct cll*next;
- struct cll*prev;
- };
- class cll_create
- {
- struct cll*head;
- public:
- cll_create()
- {
- head=NULL;
- }
- void insert_at_end();
- void insert_at_start();
- void insert_between();
- void display_cll();
- };
- void cll_create::insert_between()
- {
- struct cll*temp,*trav,*temp1;
- int node;
- if(head==NULL)
- {
- head=new cll;
- head->next=head;
- head->prev=head;
- cout<<"\nEnter the data";
- cin>>head->data;
- }
- else
- {
- temp=new cll;
- temp->next=NULL;
- temp->prev=NULL;
- cout<<"\nEnter the data";
- cin>>temp->data;
- trav=head;
- cout<<"\nAfter which node you have to enter this node ";
- cin>>node;
- while(trav->data!=node&&trav->next!=head)
- trav=trav->next;
- if(trav->data==node)
- {
- temp1=trav->next;
- trav->next=temp;
- temp->prev=trav;
- temp->next=temp1;
- temp1->prev=temp;
- }
- else
- {
- cout<<"\nThe node not found in this CLL";
- }
- }
- }
- void cll_create::insert_at_start()
- {
- struct cll*temp,*trav;
- if(head==NULL)
- {
- head=new cll;
- head->next=head;
- head->prev=head;
- cout<<"\nEnter the data";
- cin>>head->data;
- }
- else
- {
- temp=new cll;
- temp->next=NULL;
- temp->prev=NULL;
- cout<<"\nEnter the data";
- cin>>temp->data;
- trav=head;
- while(trav->next!=head)
- trav=trav->next;
- trav->next=temp;
- temp->prev=trav;
- temp->next=head;
- head->prev=temp;
- head=temp;
- }
- }
- void cll_create::insert_at_end()
- {
- struct cll*temp,*trav;
- if(head==NULL)
- {
- head=new cll;
- head->next=head;
- head->prev=head;
- cout<<"\nEnter the data";
- cin>>head->data;
- }
- else
- {
- temp=new cll;
- temp->next=NULL;
- temp->prev=NULL;
- cout<<"\nEnter the data";
- cin>>temp->data;
- trav=head;
- while(trav->next!=head)
- {
- trav=trav->next;
- }
- trav->next=temp;
- temp->prev=trav;
- temp->next=head;
- head->prev=temp;
- }
- }
- void cll_create::display_cll()
- {
- struct cll*trav;
- trav=head;
- cout<<"\n CLL is as follows :\n";
- while(trav->next!=head)
- {
- cout<<" "<<trav->data;
- trav=trav->next;
- }
- cout<<" "<<trav->data;
- }
- void main()
- {
- cll_create object;
- int choice;
- clrscr();
- while(1)
- {
- cout<<"\n1.Insert data at end";
- cout<<"\n2.Insert data at start";
- cout<<"\n3.Insert between";
- cout<<"\n4.Display CLL";
- cout<<"\n5.Exit";
- cout<<"\nEnter your choice ";
- cin>>choice;
- switch(choice)
- {
- case 1:
- object.insert_at_end();
- break;
- case 2:
- object.insert_at_start();
- break;
- case 3:
- object.insert_between();
- break;
- case 4:
- object.display_cll();
- break;
- case 5:
- exit(0);
- }
- }
- }
No comments:
Post a Comment