#include
<iostream.h>
template <class T>
class
SList
{
private:
T
data;
SList
*next,*head, *Ptr;
public:
SList()
{
head=NULL;
}
void
Insert(int);
void
Delete(int);
void
disp();
void
Rev();
};
template
<class T>
void
SList<T>::Insert(int num)
{
SList
*temp,*pr;
temp=new
SList();
temp->data=num;
temp->next=NULL;
if(head==NULL)
{
Ptr=head=temp;
}
else
{
Ptr->next=temp;
Ptr=temp;
}
}
template
<class T>
void SList<T>::Rev()
{
SList *ptr1,*ptr2,*ptr3;
ptr2=ptr3=NULL;
ptr1
= head;
while(ptr1!=NULL)
{
ptr3=ptr2;
ptr2=ptr1;
ptr1=ptr1->next;
ptr2->next=ptr3;
}
head=ptr2;
}
template
<class T>
void
SList<T>::disp()
{
SList
*pr;
cout<<"\n\tData
: ";
for(pr=head;pr!=NULL;pr=pr->next)
{
cout<<pr->data<<"
";
}
}
template
<class T>
void
SList<T>::Delete(int num)
{
SList
*pr,*ptr1;
for(pr=head;pr!=NULL;pr=pr->next)
{
if(pr->data==num)
{
break;
}
ptr1=pr;
}
if(pr==head)
{
head=head->next;
}
else
if(pr==NULL)
{
cout<<"\n\tData
not in the List \n";
}
else
{
ptr1->next=pr->next;
}
delete pr;
}
int
main()
{
int
ch=0;
int
num;
SList
<int>st;
while(ch<5)
{
cout<<"\n\t1.Add.
\n";
cout<<"\t2.Display.
\n";
cout<<"\t3.Delete.
\n";
cout<<"\t4.Reverse.
\n";
cout<<"\t5.Exit.
\n";
cout<<"\tChoice?
";
cin>>ch;
switch(ch)
{
case
1:
cout<<"\n\nEnter
a No. ";
cin>>num;
st.Insert(num);
break;
case
2:
st.disp();
break;
case 3:
cout<<"\n\nEnter
a No. ";
cin>>num;
st.Delete(num);
break;
case
4:
st.Rev();
break;
case
5:
cout<<"\n\n\TYou
Chose To Exit. \n";
break;
}
}
return
0;
}
No comments:
Post a Comment