Searching particular node in Doubly Linked List
//find()
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
template<class T>
class node
{
node<T> *llink;
T data;
node<T> *rlink;
friend class dll<T>;
};
template <class T>
class dll
{
private:
node<T> *l,*r;
public:
dll();
~dll();
int isempty();
int length();
T find(int i);
void append(node<T>*k);
void create();
};
template<class T>
dll<T>::dll()
{
l=r=NULL;
}
template<class T>
int dll<T>::isempty()
{
return(l==NULL);
}
//length()
template<class T>
int dll<T>::length()
{
int ctr=0;
node<T> *p;
p=l;
while(p!=NULL)
{
ctr++;
p=p->rlink;
}
return(ctr);
}
//find()
template<class T>
T dll<T>::find(int i)
{
node<T> *p;
if(i<1||i>length())
return(-1);
p=l;
for(int j=1;j<=i-1;j++)
p=p->rlink;
return(p->data);
}
//apend()
template <class T>
void dll<T>::append(node<T>* k)
{
node<T> *last;
k->rlink=NULL;
if(isempty())
l=r=k;
else
{
last=r;
while(last->rlink!=NULL)
{
last=last->rlink;
}
last->rlink=k;
}
}
//create()
template<class T>
void dll<T>::create()
{
T x;
node<T> *k;
l=r=NULL;
cout<<"enter values terminated by -1 or !\n";
cin>>x;
while(x!=-1&&x!='!')
{
k=new node<T>;
k->data=x;
append(k);
cin>>x;
}
}//create()
template<class T>
dll<T>::~dll()
{
}
main()
{
dll<int>a;
node<int>*p;
int ch,i,x;
clrscr();
a.create();
cout<<"enter value of i\n";
cin>>i;
x=a.find(i);
if(x==-1)
cout<<"non existing node\n";
else
cout<<"data of desired node is"<<x<<"\n";
getch();
return(0);
}//main()
Doubly Linked List Operations
//find()
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
template<class T>
class node
{
node<T> *llink;
T data;
node<T> *rlink;
friend class dll<T>;
};
template <class T>
class dll
{
private:
node<T> *l,*r;
public:
dll();
~dll();
int isempty();
int length();
T find(int i);
void append(node<T>*k);
void create();
};
template<class T>
dll<T>::dll()
{
l=r=NULL;
}
template<class T>
int dll<T>::isempty()
{
return(l==NULL);
}
//length()
template<class T>
int dll<T>::length()
{
int ctr=0;
node<T> *p;
p=l;
while(p!=NULL)
{
ctr++;
p=p->rlink;
}
return(ctr);
}
//find()
template<class T>
T dll<T>::find(int i)
{
node<T> *p;
if(i<1||i>length())
return(-1);
p=l;
for(int j=1;j<=i-1;j++)
p=p->rlink;
return(p->data);
}
//apend()
template <class T>
void dll<T>::append(node<T>* k)
{
node<T> *last;
k->rlink=NULL;
if(isempty())
l=r=k;
else
{
last=r;
while(last->rlink!=NULL)
{
last=last->rlink;
}
last->rlink=k;
}
}
//create()
template<class T>
void dll<T>::create()
{
T x;
node<T> *k;
l=r=NULL;
cout<<"enter values terminated by -1 or !\n";
cin>>x;
while(x!=-1&&x!='!')
{
k=new node<T>;
k->data=x;
append(k);
cin>>x;
}
}//create()
template<class T>
dll<T>::~dll()
{
}
main()
{
dll<int>a;
node<int>*p;
int ch,i,x;
clrscr();
a.create();
cout<<"enter value of i\n";
cin>>i;
x=a.find(i);
if(x==-1)
cout<<"non existing node\n";
else
cout<<"data of desired node is"<<x<<"\n";
getch();
return(0);
}//main()
Doubly Linked List Operations
Types of Linked list
No comments:
Post a Comment