You are on page 1of 5

//Implementation of Double Linked List.

#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class DLL;
template<class T>
class node
{
public:T ele;
node<T> *prev,*next;
friend DLL<T>;
node(T x)
{
ele=x;
prev=next=NULL;
}
};
template<class T>
class DLL
{
int ls;
node<T> *first,*last;
public:DLL()
{
first=last=NULL;
ls=0;
}
bool isempty()
{
if(first==NULL&&last==NULL)
{
return true;
}
else
{
return false;
}
}
int size()const;
void insert_atbeg(T);
void insert_atend(T);
void insert_atpos(T ,int );
int indexof(T)const;
void display()const;
void display_reverse()const;
T delete_frompos(int pos);
T delete_frombeg();
T delete_fromend();

};
template<class T>
void DLL<T>::insert_atbeg(T x)
{
node<T>*nn=new node<T>(x);
if(isempty())
{
first=last=nn;
}
else{
nn->next=first;
first->prev=nn;
first=nn;
}
ls++;
}
template<class T>
void DLL<T>::insert_atend(T x)
{
node<T>*nn=new node<T>(x);
if(isempty())
{
first=last=nn;
}
else{
nn->prev=last;
last->next=nn;
last=nn;
}
ls++;
}
template<class T>
int DLL<T>::size()const
{
node<T> *p=first;
int c=0;
while(p!=NULL)
{
p=p->next;
c++;
}
return c;
}
template<class T>
void DLL<T>::insert_atpos(T ele,int pos)
{
if(pos<0||pos>ls) //we passed an invalid position for
insertion
{
cout<<"invalid position given"<<endl;
return;
}
node<T> *nn=new node<T> (ele);
if(isempty())
{
first=last=nn;
ls++;
}
else
{
if(pos<=(int)(ls)/2)
{
if(pos==0)
{
nn->next=first;
first->prev=nn;
first=nn;
ls++;
return;
}
node<T> *p=first;
for(int i=0;i<pos-1;i++,p=p->next);
nn->next=p->next;
nn->prev=p;
nn->next->prev=nn;
p->next=nn;
}
else //pos>ls/2
{
if(pos==ls)
{
last->next=nn;
nn->prev=last;
last=nn;
ls++;
return;
}
node<T> *p=last;
for(int i=ls-1;i>=pos;i--,p=p->prev);
nn->next=p->next;
nn->prev=p;
nn->next->prev=nn;
p->next=nn;
}
ls++;
}
}

template<class T>
T DLL<T>::delete_frompos(int pos)
{
if(pos<0||pos>=ls)
{
cout<<"invalid position given"<<endl;
return 0;
}
else
{
node<T>*cur;
if(pos<=(int)(ls)/2)
{
if(pos==0)
{
cur=first;
first=first->next;
first->prev=NULL;
if(first==NULL)
last=NULL;
}
else
{
node<T>* p=NULL;
cur=first;
for(int i=0;i<pos;i++)
{
p=cur;
cur=cur->next;
}
p->next=cur->next;
cur->next->prev=p;
}
T x=cur->ele;
delete cur;
ls--;
return x;
}
else
{
node<T>* cur,*p;
if(pos==ls-1)
{
cur=last;
last=last->prev;
last->next=NULL;
}
else
{
p=NULL;cur=last;
for(int i=ls-1;i>pos;i--)
{
p=cur;
cur=cur->prev;
}
p->prev=cur->prev;
cur->prev->next=p;
}
T x=cur->ele;
delete cur;
ls--;
return x;
}
}
}

main()
{
int x,p,ch,k;
DLL <int>a;
while(1)
{
cout<<"1.isempty 2.size 3.insert 4.delete 5.index
6.show 6.exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:x=a.isempty();
if(x==1)
cout<<"list is empty"<<endl;
else
cout<<"list is not empty"
<<endl;
break;
case 2:x=a.size();
cout<<"list size is "
<<x<<endl;
break;
case 3:cout<<"enter the element and position"
<<endl;
cin>>x>>p;
a.insert_atpos(x,p);
break;
case 4:cout<<"enter the position"<<endl;
cin>>p;
a.delete_frompos(p);
break;
case 5:cout<<"enter the element whose index is
to be found"<<endl;
cin>>x;
k=a.indexof(x);
if(k!=-1)
cout<<x<<"element is fount at
position"<<k<<endl;
else
cout<<"element is not found in
list"<<endl;
break;
case 6:a.display();
break;
case 7:exit(1);
}
}
return 0;
}

You might also like