/************************* 2. Write a function {\em int size(A* head)} that returns the number of elements in a linked list {\em head}. ***********************/ #include using namespace std; class A{ public: char c; A* next; A(char x='a'){ c=x; next=NULL; } A* add_to_head(A* p){ p->next=this; return p; } void display(){ A* p=this; if(p==NULL){ cout<<"Empty"<display(); show_size(head); head=head->add_to_head(&a1); head->display(); show_size(head); head=head->add_to_head(&a2); head->display(); show_size(head); head=head->add_to_head(&a4); head->display(); show_size(head); head=head->add_to_head(&a7); head->display(); head=head->add_to_head(&a5); head->display(); show_size(head); head=head->add_to_head(&a3); head->display(); show_size(head); } void show_size(A* head){ int size=0; if(head){ size=1; while( (head->next) != NULL){ size++; head=head->next; } } cout<<"Size = "<