You are on page 1of 3

#include<iostream.

h>
#include<conio.h>
class dnode
{
public:
int data;
dnode *next,*prev;
dnode()
{
next=NULL;
prev=NULL;
}
dnode(int x)
{
next=NULL;
prev=NULL;
data=x;
}
};
class dbl
{
dnode *head;
public:
dbl()
{
head=NULL;
}
void create();
void display();
int search(int x);
};
void dbl::create()
{
int x,n,i;
dnode *p;
cout<<"\n\7Enter No OF Nodes To be Created :: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<'\7';
cout<<"\n\7Enter The Value for node ["<<i<<"] :: ";
cin>>x;
if(head==NULL)
{
head=p=new dnode(x);
}
else
{

p->next=new dnode(x);
p->next->prev=p;
p=p->next;
}
}
}
void dbl::display()
{
for(dnode *p=head;p!=NULL;p=p->next)
{
cout<<"\n _________
";
cout<<"\n| | | |
";
cout<<"\n| | "<<p->data<<" | |
";
cout<<"\n|__|___|__|
";
}
}
int dbl::search(int x)
{
int i=0;
for(dnode *p=head;p!=NULL;p=p->next)
{
if(p->data==x)
return (i);
i++;
}
return -1;
}

int main()
{
dbl d;
int x,loc;
clrscr();
cout<<"\n=======================================================================
========";
cout<<"\n |
Doubly Linked List
|";
cout<<"\n=======================================================================
========";
d.create();
d.display();
cout<<endl;
cout<<"\nenter element to be searched :: ";
cin>>x;
loc=d.search(x);
if(loc==-1)
cout<<"\nElement is Not Present";
else

cout<<"\nElement found at location :: "<<loc+1;


return 0;
getch();
};

You might also like