You are on page 1of 7

#include <iostream>

using namespace std;


template<class tem>
class cslnode
{
public:
tem information;
cslnode<tem>*next;
cslnode(tem n)
{
information=n;
next=this;
}
};
template<class tem>
class csllist
{
public:

//cslnode<tem>*head=0 ;
cslnode<tem>*tail=0;
void inserttail(tem x)
{

if(tail==0)
{
cslnode<tem>*temp=new cslnode<tem>(x);
tail=temp;
tail->next=temp;
}
else
{
/* cout<<"ENTER ELEMENT"<<endl;
cin>>x;*/
cslnode<tem>*temp=new cslnode<tem>(x);
temp->next=tail->next;
tail->next=temp;
tail=temp;
}
}
void inserthead(tem x)
{

if(tail==0)
{
/*cout<<"ENTER ELEMENT"<<endl;
cin>>x;*/
cslnode<tem>*temp=new cslnode<tem>(x);
tail=temp;
tail->next=temp;
}
else
{
/*cout<<"ENTER ELEMENT"<<endl;
cin>>x;*/
cslnode<tem>*temp=new cslnode<tem>(x);
temp=tail->next;
temp->next=temp;
tail->next=temp;
}
}
void traverse()
{
cslnode<tem>*temp=tail->next;
cout<<temp->information<<endl;
temp=temp->next;
while(temp!=tail->next)
{
cout<<temp->information<<endl;
temp=temp->next;
}
}
bool search1(tem element)
{
int find1=0;
cslnode<tem>*temp=tail->next;
while(temp!=0)
{
if(temp->information==element)
{
find1=1;
break;
}
else
{
temp=temp->next;
}
}
if(find1==1)
return true;
else
return false;
}
cslnode<tem>*search2(tem element)
{
cslnode<tem>*temp=tail->next;
do
{
if(temp->information==element)
{
return temp;
}
temp=temp->next;
}
while(temp!=tail->next);
return 0;
}
int delete1()
{
cslnode<tem>*temp=tail->next;
if(temp==0)
return 0;
else if(tail->next==tail)
{
delete(tail);
tail=0;
tail->next=0;
}
else
{
delete(temp);
tail->next=tail->next->next;
}
}
int county()
{
int c=0;
cslnode<tem>*temp=tail->next;
if(tail->next==0)
return 0;
else
{
do
{
c++;
temp=temp->next;
}
while(temp!=tail->next);
return c;
}
}
int average()
{
int c=0,sum=0;double av=0;
cslnode<tem>*temp=tail->next;
if(temp==0)
return 0;
else
{
do
{
c++;
sum=sum+(temp->information);
temp=temp->next;
}
while(temp!=tail->next);
av=sum/c;
return av;
}
}
void oddvalued()
{
cslnode<tem>*temp=tail->next;
if(temp==0)
cout<<"NO ITEMS"<<endl;
else
{
do
{
if(((temp->information)%2)!=0)
{
cout<<temp->information<<endl;
}
temp=temp->next;
}
while(temp!=tail->next);
}
}
void oddpositioned()
{
cslnode<tem>*temp=tail->next;
if(temp==0)
cout<<"NO ELEMENT FOUND"<<endl;
else if(tail->next==tail)
{
cout<<temp->information<<endl;
}
else
{
do
{
cout<<temp->information<<endl;
temp=temp->next->next;
}
while(temp!=tail->next);
}

};
int main()
{
int x;
char ch;
csllist<int>obj;
csllist<char>obj2;
int choice,choice2;
cout<<"ENTER 1 FOR INTEGER BASED"<<endl;
cout<<"ENTER 2 FOR CHARACTER BASED"<<endl;
cin>>choice2;
switch(choice2)
{
case 1:
{
do
{
cout<<"ENTER 1 TO ENTER TO TAIL"<<endl;
cout<<"ENTER 2 TO ENTER TO HEAD"<<endl;
cout<<"ENTER 3 TO TRAVERSE"<<endl;
cout<<"ENTER 4 TO ONLY SEARCH"<<endl;
cout<<"ENTER 5 TO SEARCH AND RETURN THE VALUE OF POINTER"<<endl;
cout<<"ENTER 6 TO DELETE"<<endl;
cout<<"ENTER 7 TO COUNT THE NUMBER OF NODES"<<endl;
cout<<"ENTER 8 TO FIND THE AVERAGE OF ALL THE INFORMATION"<<endl;
cout<<"ENTER 9 TO PRINT THE ODD POSITIONED ELEMENT"<<endl;
cout<<"ENTER 10 TO PRINT THE ODD VALUED ELEMENTS"<<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.traverse();
break;}
case 4:{
int ele;
cout<<"ENTER THE INFORMATION YOU WANT TO SEARCH"<<endl;
cin>>ele;
cout<<obj.search1(ele)<<endl;
break;}
case 5:{
int ele;
cout<<"ENTER THE INFORMATION YOU WANT TO SEARCH"<<endl;
cin>>ele;
cout<<obj.search2(ele)<<endl;
break;}
case 6:{
obj.delete1();
break;}
case 7:{
cout<<obj.county();
cout<<endl;
break;}
case 8:{
cout<<obj.average();
cout<<endl;
break;}
case 9:
{
cout<<"ODDPOSITIONED ELEMENTS ARE"<<endl;
obj.oddpositioned();
break;
}
case 10:
{
cout<<"ODDVALUED ELEMENTS ARE"<<endl;
obj.oddvalued();
break;
}
default:{
cout<<"invalid choice,press 0 to exit and any other key to
continue"<<endl;}
}
}
while(choice!=0);
break;
}
case 2:
{ do
{
cout<<"ENTER 1 TO ENTER TO TAIL"<<endl;
cout<<"ENTER 2 TO ENTER TO HEAD"<<endl;
cout<<"ENTER 3 TO TRAVERSE"<<endl;
cout<<"ENTER 4 TO ONLY SEARCH"<<endl;
cout<<"ENTER 5 TO SEARCH AND RETURN THE VALUE OF POINTER"<<endl;
cout<<"ENTER 6 TO DELETE"<<endl;
cout<<"ENTER 7 TO COUNT THE NUMBER OF NODES"<<endl;
// cout<<"ENTER 8 TO FIND THE AVERAGE OF ALL THE INFORMATION"<<endl;
cout<<"ENTER 9 TO PRINT THE ODD POSITIONED ELEMENTS"<<endl;
cout<<"ENTER 0 TO EXIT"<<endl;
cin>>choice;
switch(choice)
{
case 1:{
cout<<"ENTER ELEMENT"<<endl;
cin>>ch;
obj2.inserttail(ch);
break;}
case 2:{
cout<<"ENTER ELEMENT"<<endl;
cin>>ch;
obj2.inserthead(ch);
break;}
case 3:{
obj2.traverse();
break;}
case 4:{
char ele;
cout<<"ENTER THE INFORMATION YOU WANT TO SEARCH"<<endl;
cin>>ele;
cout<<obj2.search1(ele)<<endl;
break;}
case 5:{
char ele;
cout<<"ENTER THE INFORMATION YOU WANT TO SEARCH"<<endl;
cin>>ele;
cout<<obj2.search2(ele)<<endl;
break;}
case 6:{
obj2.delete1();
break;}
case 7:{
cout<<obj2.county();
cout<<endl;
break;}
/*case 8:{
cout<<obj.average();
cout<<endl;
break;}*/
case 9:
{
cout<<"ODD POSITIONED ELEMENTS"<<endl;
obj2.oddpositioned();
break;
}
default:{
cout<<"invalid choice,press 0 to exit and any other key to
continue"<<endl;}
}
}
while(choice!=0);

break;
}
default:
{
cout<<"INVALID CHOICE"<<endl;
}
}

return 0;
}

You might also like