Insertion node at particular position:
//insert
#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() ;
void display();
int length();
void insert(int i, T x);
};
template<class T>
dll<T>::dll()
{
l=r=NULL;
}
template<class T>
int dll<T>::isempty()
{
return(l==NULL);
}
template<class T>
void dll<T>::display()
{
node<T> *p;
if(isempty())
{
cout<<"DLL is empty\n";
}
else
{
cout<<"DLL is as follows\n";
for(p=l;p!=NULL;p=p->rlink)
cout<<p->llink<<"\t"<<p->data<<"\t"<<p->rlink<<"\n";
}
}
//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);
}
//insert()
template<class T>
void dll<T>::insert(int i,T x)
{
node<T> *temp,*p,*next;
if(i<0||i>length())
cout<<"Non existing node\n";
else
{
p=new node<T>;
p->data=x;
if(isempty())
{
l=r=p;
p->llink=p->rlink=NULL;
}
else if(i==0)
{
l->llink=p;
p->rlink=l;
p->llink=NULL;
l=p;
}
else if(i==length())
{
p->rlink=NULL;
p->llink=r;
r->rlink=p;
r=p;
}
else
{
temp=l;
for(int j=1;j<=i-1;j++)
temp=temp->rlink;
next=temp->rlink;
p->llink=temp;
p->rlink=next;
next->llink=p;
}
}
}
template<class T>
dll<T>::~dll()
{
}
main()
{
dll<int>a;
node<int>*p;
int ch,i,x;
clrscr();
cout<<"enter value of i( 0 to insert ar the beginning)\n";
cin>>i;
cout<<"enter value of new node\n";
cin>>x;
a.insert(i,x);
a.display();
getch();
return(0);
}//main()
Doubly Linked List Operations
Node
class structures
//insert
#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() ;
void display();
int length();
void insert(int i, T x);
};
template<class T>
dll<T>::dll()
{
l=r=NULL;
}
template<class T>
int dll<T>::isempty()
{
return(l==NULL);
}
template<class T>
void dll<T>::display()
{
node<T> *p;
if(isempty())
{
cout<<"DLL is empty\n";
}
else
{
cout<<"DLL is as follows\n";
for(p=l;p!=NULL;p=p->rlink)
cout<<p->llink<<"\t"<<p->data<<"\t"<<p->rlink<<"\n";
}
}
//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);
}
//insert()
template<class T>
void dll<T>::insert(int i,T x)
{
node<T> *temp,*p,*next;
if(i<0||i>length())
cout<<"Non existing node\n";
else
{
p=new node<T>;
p->data=x;
if(isempty())
{
l=r=p;
p->llink=p->rlink=NULL;
}
else if(i==0)
{
l->llink=p;
p->rlink=l;
p->llink=NULL;
l=p;
}
else if(i==length())
{
p->rlink=NULL;
p->llink=r;
r->rlink=p;
r=p;
}
else
{
temp=l;
for(int j=1;j<=i-1;j++)
temp=temp->rlink;
next=temp->rlink;
p->llink=temp;
p->rlink=next;
next->llink=p;
}
}
}
template<class T>
dll<T>::~dll()
{
}
main()
{
dll<int>a;
node<int>*p;
int ch,i,x;
clrscr();
cout<<"enter value of i( 0 to insert ar the beginning)\n";
cin>>i;
cout<<"enter value of new node\n";
cin>>x;
a.insert(i,x);
a.display();
getch();
return(0);
}//main()
Doubly Linked List Operations
Types of Linked list
No comments:
Post a Comment