Doubly Linked List
Doubly Linked List
#include <iostream.h>
#include <stdlib.h> //for exit(1)
#include <conio.h>
#define MAX 10
struct node{
int data;
struct node *lptr;
struct node *rptr;
};
class dbllist{
node *head;
public:
dbllist(){
head=NULL;
}
void create(); //initial data assignmentvoid display(int); //process is
display (assumed)int count(int);
void insert();
void del();
void search();
void reverse();
void sort();
};
head->rptr = start;
start->lptr = head;
head = start;
}
cout<<"\n\t\t*****Display*****\n";
cout<<"\t\t";
if(check==1){ //forward displayfor(tmp=head; tmp!=NULL; tmp=tmp->rptr){
cout<<tmp->data;
if(tmp->rptr != NULL)
cout<<"-->";
}//end for
}
else{ //Reverse displayfor( tmp=head; tmp->rptr!=NULL; tmp=tmp-
>rptr);//points to last nodewhile(tmp!=NULL)
{
cout<<tmp->data;
if(tmp->lptr != NULL)
cout<<"-->";
tmp = tmp->lptr;
}//end whiile
}//end if
getch();
}
switch(choice){
case 1 : newl->rptr = head;
head->lptr = newl;
head = newl;
break;
next = prev->rptr;
newl->rptr = next;
next->lptr = newl;
newl->lptr = prev;
prev->rptr = newl;
break;
}
case 3 : //For pointing to last nodefor(tmp=head; tmp->rptr != NULL;
tmp=tmp->rptr);
tmp->rptr = newl;
newl->lptr = tmp;
}//end switch
}//end while
}
next=prev->rptr->rptr;
delnode = prev->rptr;
prev->rptr = prev->rptr->rptr;
next->lptr = prev;
break;
}
case 3 : //For pointing to last nodefor(tmp=head; tmp->rptr->rptr !=
NULL; tmp=tmp->rptr);
delnode = tmp->rptr;
tmp->rptr = NULL;
break;
case 4 : return;
default : cout<<"\n\t\tInvalid Position";
getch();
}//end switch
delete(delnode);
}//end while
}
void main()
{
int choice;
dbllist obj;
while(1){
clrscr();
cout<<"\n\t\tDOUBLY LINK-LIST OPERATIONS\n\n";
cout<<"\t\t1) Create List\n";
cout<<"\t\t2) Display List\n";
cout<<"\t\t3) List Status\n";
cout<<"\t\t4) List Insertion\n";
cout<<"\t\t5) List Deletion\n";
cout<<"\t\t6) Search List\n";
cout<<"\t\t7) Display Reverse List\n";
cout<<"\t\t8) Sort List\n";
cout<<"\t\t9) Exit\n";
cout<<"\t\tEnter your Choice : ";
cin>>choice;
switch(choice){
case 1 : obj.create(); // 1 for A listbreak;
case 2 : obj.display(1);// 1 for A listbreak;
case 3 : choice = obj.count(1);
//choice value is not used anywhere//it is just a placeholderbreak;
case 4 : obj.insert();
break;
case 5 : obj.del();
break;
case 6 : obj.search();
break;
case 7 : obj.display(2);
break;
case 8 : obj.sort();
break;
case 9 : gotoout;
default: cout<<"\n\n\t\tInvalid Choice\n\n";
getch();
break;
}
}
out:
}