Linear List
Linear List
class ArOutOfBound
{
public: char msg[30];
ArOutOfBound()
{
strcpy(msg,"Array out of bound!!!");
}
};
class NoMem
{
public: char msg[30];
NoMem()
{
strcpy(msg,"No Memory!!!");
}
};
template<class T>
class Linlist
{
int length;
int maxsize;
T *ar;
public: Linlist(int);
~Linlist();
};
template<class T>
Linlist<T>::Linlist(int msize)
{
length=0;
maxsize=msize;
ar=new T[maxsize];
}
template<class T>
Linlist<T>::~Linlist()
{
delete[] ar;
}
template<class T>
bool Linlist<T>::IsEmpty() const
{
return length==0;
}
template<class T>
int Linlist<T>::Length() const
{
return length;
}
template<class T>
Linlist<T>& Linlist<T>::insert(int k,const int& x)
{
try
{
if(k<0 || k>length)
throw ArOutOfBound();
if(length==maxsize)
throw NoMem();
for(int i=length-1;i>=k;i--)
ar[i+1]=ar[i];
ar[k]=x;
length++;
return *this;
}
catch(ArOutOfBound b)
{
cout<<b.msg<<endl;
}
catch(NoMem b)
{
cout<<b.msg<<endl;
}
template<class T>
bool Linlist<T>::find(int k,int& x) const
{
if(k<1 || k>length)
return false;
x=ar[k-1];
return true;
template<class T>
Linlist<T>& Linlist<T>::Delete(int k,int& x)
{
try
{
if(find(k,x))
{
for(int i=k;i<length;i++)
ar[i-1]=ar[i];
length--;
return *this;
}
else
throw ArOutOfBound();
}
catch(ArOutOfBound b)
{
cout<<b.msg<<endl;
}
}
template<class T>
int Linlist<T>::Search(const int& x) const
{
for(int i=0;i<length;i++)
{
if(ar[i]==x)
return ++i;
}
return 0;
}
template<class T>
void Linlist<T>::output() const
{
cout<<"List contains:"<<endl;
for(int i=0;i<length;i++)
cout<<ar[i]<<"\t";
cout<<endl;
}
main()
{
Linlist<int> l(5);
cout<<"---------------------------------------"<<endl;
cout<<"Checking whether list is empty:"<<endl;
cout<<l.IsEmpty()<<" (1:Empty 0:Not Empty)"<<endl;
cout<<"---------------------------------------"<<endl;
cout<<"Displaying the Length of the list:"<<endl;
cout<<l.Length()<<endl;
cout<<"---------------------------------------"<<endl;
l.insert(0,5);
l.insert(1,10);
l.insert(2,20);
l.insert(3,25);
cout<<"After insertion ";
l.output();
cout<<"----------------------------------------"<<endl;
cout<<"Checking whether list is empty:"<<endl;
cout<<l.IsEmpty()<<" (1:Empty 0:Not Empty)"<<endl;
cout<<"---------------------------------------"<<endl;
cout<<"Displaying the Length of the list:"<<endl;
cout<<l.Length()<<endl;
cout<<"---------------------------------------"<<endl;
cout<<"Inserting element where an element is already present"<<endl;
l.insert(2,15);
l.output();
cout<<"-------------------------------------------------------"<<endl;
cout<<"Exception Generation during insertion"<<endl;
l.insert(3,30);
l.insert(6,35);
cout<<"---------------------------------------"<<endl;
cout<<"After Deletion.."<<endl;
int z=15;
l.Delete(3,z);
l.output();
z=35;
l.Delete(1,z);
l.output();
cout<<"----------------------------------------"<<endl;
cout<<"Exception Generation during deletion:"<<endl;
l.Delete(-3,z);
cout<<"----------------------------------------"<<endl;
cout<<"Reasult of Searching an element:"<<endl;
cout<<l.Search(60)<<" (0:Failed Otherwise:Successful & is the position in the
List)"<<endl;
cout<<l.Search(20)<<" (0:Failed Otherwise:Successful & is the position in the
List)"<<endl;
}