You are on page 1of 8

#include <iostream>

using namespace std;


template<class tem>
class dllnode
{
public:
int info;
dllnode<tem>*next;
dllnode<tem>*prev;
dllnode(tem n)
{
info=n;
}
};
template<class tem>
class dllist
{
public:
dllnode<tem>*head=0;
dllnode<tem>*tail=0;
void inserttail(tem x)
{
if(head==0)
{
dllnode<tem>*temp=new dllnode<tem>(x);
head=tail=temp;
}
else
{
dllnode<tem>*temp=new dllnode<tem>(x);
tail->next=temp;
temp->prev=tail;
temp->next=0;
tail=temp;
}
}
void inserthead(tem x)
{
if(head==0)
{
dllnode<tem>*temp=new dllnode<tem>(x);
head=tail=temp;
}
else
{
dllnode<tem>*temp=new dllnode<tem>(x);
temp->next=head;
temp->prev=0;
head->prev=temp;
head=temp;
}

}
void traverseback()
{
dllnode<tem>*temp=tail;
while(temp!=0)
{
cout<<temp->info<<endl;
temp=temp->prev;
}
}
dllnode<tem>*search2(int element)
{
dllnode<tem>*temp=head;
while(temp!=0)
{
if(temp->info==element)
{
return temp;
}
temp=temp->next;
}
return 0;
}
void middleele()
{

dllnode<tem>*temp1=head;
dllnode<tem>*temp2=tail;
dllnode<tem>*temp3=0;
if(temp1==0)
cout<<"NO ELEMENT FOUND"<<endl;

else if(head==tail)
{
cout<<temp1->info<<endl;
}
else
{
while(temp1!=temp2)
{
if(temp1->next==temp2)
{
//cout<<temp1->info<<endl;
cout<<temp2->info<<endl;
break;
}
else
{
temp1=temp1->next;
temp2=temp2->prev;
}
}
cout<<temp1->info<<endl;
}
}
void oddvalued()
{
dllnode<tem>*temp=head;
if(temp==0)
cout<<"NO ITEMS"<<endl;
else
{
while(temp!=0)
{
if(((temp->info)%2)!=0)
{
cout<<temp->info<<endl;
}
temp=temp->next;
}
}
}
void oddpositioned()
{
dllnode<tem>*temp=head;
if(temp==0)
cout<<"NO ELEMENT FOUND"<<endl;
else if(head==tail)
{
cout<<temp->info<<endl;
}
else
{
while(temp!=0)
{
cout<<temp->info<<endl;
temp=temp->next->next;
}
}

}
bool palindrome()
{
dllnode<tem>*temp1=head;
dllnode<tem>*temp2=tail;
while(temp1!=temp2)
{
if(temp1->info==temp2->info)
{
if(temp1->next==temp2)
return true;

temp1=temp1->next;
temp2=temp2->prev;
}
else
return false;
}
return true;

}
int insertAfternpos(int pos,int x)
{
dllnode<tem>*temp = head;
int index = 0;
int c=0;
if(temp==0)
{
cout<<"LIST IS EMPTY"<<endl;
return -1;
}
else
{
while(temp!=0)
{

index++;
if(index==pos)
{
c=1;
break;
}
temp = temp->next;
}
if(temp==tail)
{
dllnode<tem>*temp2= new dllnode<tem>(x);
temp2->prev = tail;
tail->next = temp2;
tail = temp2;
}
else
{
dllnode<tem>*temp2= new dllnode<tem>(x);
temp->next->prev = temp2;
temp2->next = temp->next;
temp->next = temp2;
temp2->prev = temp;
}

}
return 0;
}
int insertbeforenpos(int pos,int x)
{
dllnode<tem>*temp = head;
int index = 0;
int c=0;
if(head==0)
{
cout<<"EMPTY LIST"<<endl;
return-1;
}
else
{
while(temp!=0)
{
if(pos!=1)
{
index++;
}
if(index==pos-1)
{
c=1;
break;
}
temp = temp->next;
}
if(temp==head)
{
dllnode<tem>*temp2= new dllnode<tem>(x);
temp->prev = tep2;
temp2->next = head;
head = temp2;
}
else
{
dllnode<tem>*temp2= new dllnode<tem>(x);
temp->next->prev = temp2;
temp2->next = temp->next;
temp->next = temp2;
temp2->prev = temp;
}

}
return 0;
}
int deleteafternpos(int pos)
{
dllnode<tem>*temp = head;
int index = 0;
int c=0;
if(head==0)
{
cout<<"EMPTY LIST"<<endl;
return 1;
}
if(head==tail)
{
cout<<"NODE IS NOT AVAILABLE"<<endl;
return 1;
}
if(head!=tail)
{
while(temp!=NULL)
{
index++;
if(index==pos+1)
{
c=1;
break;
}
temp = temp->next;
}
if(c==1)
{
if(temp==tail)
{
temp->prev->next =0;
tail = temp->prev;
delete temp;
return 0;
}
else if(temp==head)
{
temp->next->prev = 0;
head = temp->next;
delete temp;

}
else{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
}
else
{
cout<<"INVALID"<<endl;
return -1;
}
}
return 0;
}
int deletebeforenpos(int pos)
{
dllnode<tem>*temp = head;
int index = 0;
int c=0;
if(head==0)
{
cout<<"EMPTY LIST"<<endl;
return -1;
}
if(pos==1 || head==tail)
{
cout<<"NODE NOT AVAILABLE"<<endl;
return -1;
}
if(head!=tail && pos!=1)
{
while(temp!=NULL){
index++;
if(index==pos-1)
{
c=1;
break;
}
temp = temp->next;
}
if(c==1)
{
if(temp==head)
{
temp->next->prev = NULL;
head = temp->next;
delete temp;
}
else if(temp==tail)
{
tail->prev->next = NULL;
temp = tail->prev;
delete tail;
tail = temp;
}
else
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
}
else
{
cout<<"INVALID"<<endl;
return -1;
}
}
return 0;
}

};
int main()
{
int x;int p;
dllist<int>obj;
//dllist<char>obj2;
int choice;
do
{
cout<<"ENTER 1 TO ENTER TO TAIL"<<endl;
cout<<"ENTER 2 TO ENTER TO HEAD"<<endl;
cout<<"ENTER 3 TO TRAVERSE BACKWARD"<<endl;
cout<<"ENTER 4 TO SEARCH AND RETURN THE VALUE OF POINTER"<<endl;
cout<<"ENTER 5 TO FIND THE MIDDLE ELEMENT"<<endl;
cout<<"ENTER 6 TO PRINT THE ODD VALUED ELEMENT"<<endl;
cout<<"ENTER 7 TO PRINT THE ODD POSITIONED ELEMENTS"<<endl;
cout<<"ENTER 8 TO CHECK WHETHER IT IS PALINDROME OR NOT"<<endl;
cout<<"ENTER 9 TO INSERT AFTER GIVEN POSITION"<<endl;
cout<<"ENTER 10 TO INSERT BEFORE GIVEN POSITION"<<endl;
cout<<"ENTER 11 TO DELETE AFTER GIVEN POSITION"<<endl;
cout<<"ENTER 12 TO DELETE BEFORE GIVEN POSITION"<<endl;
cout<<"ENTER 0 TO EXIT"<<endl;
cin>>choice;
switch(choice)
{
case 1:{
cout<<"ENTER ELEMENT"<<endl;
cin>>x;
obj.inserttail(x);
break;}
case 2:{
cout<<"ENTER ELEMENT"<<endl;
cin>>x;
obj.inserthead(x);
break;}
case 3:{
obj.traverseback();
break;}
case 4:{
int ele;
cout<<"ENTER THE INFORMATION YOU WANT TO SEARCH"<<endl;
cin>>ele;
cout<<obj.search2(ele)<<endl;
break;}
case 5:
{
cout<<"MIDDLE ELEMENT IS "<<"\t";
obj.middleele();
break;
}
case 6:
{
cout<<"ODD VALUED ELEMENTS ARE"<<endl;
obj.oddvalued();
break;
}
case 7:
{
cout<<"ODD POSITIONED ELEMENTS"<<endl;
obj.oddpositioned();
break;
}
case 8:
{
cout<<obj.palindrome();
break;
}
case 9:
{
cout<<"ENTER POSITION"<<endl;
cin>>p;
cout<<"ENTER VALUE"<<endl;
cin>>x;
obj.insertAfternpos(p,x);
break;
}
case 10:
{
cout<<"ENTER POSITION"<<endl;
cin>>p;
cout<<"ENTER VALUE"<<endl;
cin>>x;
obj.insertbeforenpos(p,x);
break;
}
case 11:
{
cout<<"ENTER POSITION"<<endl;
cin>>p;
obj.deleteafternpos(p);
break;
}
case 12:
{
cout<<"ENTER POSITION"<<endl;
cin>>p;
obj.deletebeforenpos(p);
break;
}
default:{
cout<<"invalid choice,press 0 to exit and any other key to
continue"<<endl;}
}
}
while(choice!=0);
return 0;
}

You might also like