#include<iostream>
using namespace std;
class Empty{
      string origin;
      public:
          Empty(string s): origin(s){ };
          string getString(){
             return origin;       
          }   
};

template <class atype>
struct Node{
    atype data;
    Node* next;       
};
template <class atype>
class Linklist{
     private:
        Node<atype>* first;
     public:
        Linklist(): first(NULL){ }
        void additem(atype x)
        {
           Node<atype>* newptr = new Node<atype>;
           newptr->data = x;
           newptr->next = NULL;  
           if(first == NULL)
              first = newptr; 
           else{
             Node<atype>* current = first;
             while(current->next != NULL)
                current = current->next;
             current->next = newptr;
           }
        }
        void display()
        {
           Node<atype>* current = first;
           if(first == NULL)
               throw Empty("display()");
           cout<<"List is below "<<endl;      
           while(current != NULL)
           {
                cout<<current->data<<endl;
                current= current->next;              
           }   
        }
        void del(){
            if(first == NULL )
              throw Empty("del()");
            Node<atype>* current = first;
            Node<atype>* previous = current;
            while(current->next != NULL){
                  previous = current;
                  current=current->next;
            }
            if(current == first)
                  first = NULL;
            else
                 previous->next = NULL; 
            delete current;       
        }              
};
main(){
  try{     
  Linklist<int> l1;
  //l1.del();
  l1.display();
  l1.additem(3);
  l1.additem(4);
  l1.additem(5);
  l1.additem(6);
  l1.display();
  l1.del();
  l1.del();
  //l1.del();
  //l1.del();
  //l1.del();
  l1.display();
  }
  catch(Empty e){
      cout<<"List is empty. ";        
      cout<<"Origin of exception "<<e.getString()<<endl;            
  }
  system("pause");       
}
