Program of Doubly linked list using C++

// Doubly linked list //

#include
#include
#define NULL 0
struct list
{
int data;
struct list*next;
struct list *prev;
};
typedef struct list node;
node *find(node *,int);
node *pre=NULL;
node *fin=NULL;
node *start;
int main()
{
int menu(void);
void create(node *);
void display(node *);
void insert(node *,int,int);
void revdisplay(node *);
void del(node *,int);
int choice,ch;
int data,tar;
node *newrec;
start=NULL;


do
{
clrscr();
choice=menu();
switch(choice)
{
case '1':
cout<<"\nCreating the list"; cout<<"Enter the terms(type 0 to end)\n"; start=new node; create(start); break; case '2': if (start==NULL) cout<<"\nList does not exist"; else { cout<<"\nDisplaying the list\n"; display(start); } getch(); break; case '3': if (start==NULL) { cout<<"\nList does not exist"; getch(); } else { cout<<"\nDisplaying the list:"; revdisplay(fin); case '4': if(start==NULL) { cout<<"\nList does not exist"; getch(); } else { cout<<"\nEnter the term to insert:"; cin>>data;
cout<<"\nEnter target term:"; cin>>tar;
insert(start,data,tar);
}
break;
case '5':
if(start==NULL)
cout<<"\nlist does not exist:"; getch(); } else { cout<<"\nEnter the term to delete:"; cin>>data;
del(start,data);
break;
case '6':
cout<<"\nExiting"; break; default: cout<<"\nNot a valid choice"; getch(); break; } }while(choice!=6); getch(); return(0); } int menu(void) { int ch; cout<<"\n1->Creation of the list";
cout<<"\n2->Displaying of the list";
cout<<"\n3->Displaying the list";
cout<<"\n4->Insertion of the list";
cout<<"\n5->Deletion of the list";
cout<<"\n6->Reverse of the list";
cout<<"\n7->Exit";
cout<<"\nEnter your choice:"; cin>>ch;
return(ch);
}
void create(node *record)
{
cin>>record->data;
if(record->data==0)
{
record->next=NULL;
record->prev=pre;
fin=record->prev;
pre=NULL;
else
{
record->prev=pre;
record->next=new node;
pre=record;
create(record->next);
}
return;
}
void display(node *record)
{
if(record->next!=NULL)
{
cout<data<next);
}
return;
}
void reverse(node *record)
{
if(record!=NULL)
{
cout<data<prev);
}
return;
void insert(node *record,int data,int target)
{
node *tag,*newrec,*temp;
newrec=new node;
if(record->data==target)
{
newrec->next=record;
newrec->prev=NULL;
record->prev=newrec;
start=newrec;
}
else
{
tag=find(record,target);
temp=tag->prev;
tag->prev=newrec;
newrec->next=tag;
temp->next=newrec;
}
if(tag==NULL)
{
cout<<"Target item not present in the list\n"; getch(); return; } newrec->data=data;
return;
}
void del(node *record,int target)
{
node *tag,*temp;
if(record->data==target)
{
temp=record;
start=start->next;
start->prev=NULL;
delete temp;
}
else
{
tag=find(record,target);
if(tag->next->next==NULL)
{
fin=fin->prev;
fin->next=tag->next;
}
if(tag==NULL)
{
cout<<"Target item not present in the list\n"; getch(); return; } return; } node *find(node *record,int target) { if(record->next->data==target)
{
return(record);
}
else if(record->next==NULL)
return(NULL);
else
find(record->next,target);
}
}

Post a Comment

Previous Post Next Post