#include<iostream>
using namespace std;
template<class T>
class linear_list
{
T *p;
int size;
public:
void set();
T get();
int getindex(T);
void del(T);
T findmax();
T findmin();
void empty();
void display();
};
template<class T>
void linear_list<T>::set()
{
cout<<"\nEnter size of array\n";
cin>>size;
p=new T[size];
cout<<"\nEnter elements\n";
for(int i=0; i<size; i++)
cin>>*(p+i);
}
template<class T>
T linear_list<T>::get()
{
if(size<1)
{
cout<<"\nList is empty";
return -1;
}
else
return *p;
}
template<class T>
int linear_list<T>::getindex(T ele)
{
if(size<1)
{
cout<<"\nList is empty";
return -1;
}
else
{
for(int i=0; i<size; i++)
{
if(*(p+i)==ele)
return (i+1);
}
}
return -1;
}
template<class T>
void linear_list<T>::del(T ele)
{
int pos;
pos=getindex(ele);
for(int i=pos-1; i<size; i++)
*(p+i)=*(p+i+1);
size=size-1;
}
template<class T>
T linear_list<T>::findmax()
{
int max=*p;
for(int i=1; i<size; i++)
{
if(max<*(p+i))
{
max=*(p+i);
}
}
return max;
}
template<class T>
T linear_list<T>::findmin()
{
int min=*p;
for(int i=1; i<size; i++)
{
if(min>*(p+i))
{
min=*(p+i);
}
}
return min;
}
template<class T>
void linear_list<T>::empty()
{
for(int i=0; i<size; i++)
{
*(p+i)=0;
}
}
template<class T>
void linear_list<T>::display()
{
cout<<"\nArray is\n";
for(int i=0; i<size; i++)
cout<<*(p+i)<<" ";
cout<<"\n";
}
int main()
{
int ele, pos, option, n=1;
linear_list<int> ob;
while(n==1)
{
cout<<"\nEnter option";
cout<<"\n1 set element\n2 get element\n3 getindex of element\n4 delete an element \n5 find maximum element\n6 find minimum element\n7 empty the array\n0 exit program\n";
cin>>option;
switch(option)
{
case 1:
ob.set();
ob.display();
break;
case 2:
ob.get();
ob.display();
break;
case 3:
cout<<"\nEnter element to get index of\n";
cin>>ele;
pos=ob.getindex(ele);
if(pos!=0)
{
cout<<"\nElement found at "<<pos;
}
else
cout<<"\nElement not found";
break;
case 4:
cout<<"\nEnter element to be deleted";
cin>>ele;
ob.del(ele);
ob.display();
break;
case 5:
cout<<"\nMaximum element is "<<ob.findmax()<<"\n";
break;
case 6:
cout<<"\nMinimum element is "<<ob.findmin()<<"\n";
break;
case 7:
ob.empty();
ob.display();
break;
case 0:
n=0;
break;
}
}
system("pause");
}
No comments:
Post a Comment